Variables
A few important points here. First, spaces are not permitted on either
side of the equals sign.
Second, unlike most other programming languages, the shell has no
concept whatsoever of data types.
Because the shell has no concept of data types, variables are not
declared before they're used;
Displaying the Values of Variables
The $ character is a special character to the shell. If a valid variable
name follows the $,
the shell takes this as an indication that the value stored
inside that variable is to be substituted at that point.
(Remember, the shell performs variable substitution before it executes
the command )
The Null Value
A variable that contains no value is said to contain the null value.
It is the default case for variables that you never store values in.
When the shell performs its variable substitution, any values that are
null are completely removed
from the command line, without a trace:
The ${variable} Construct
The shell thinks filenameX is the full name of the variable because
it's composed entirely of valid variable name characters.
To avoid this problem, you can delimit the end of the variable name by
enclosing the entire name
(but not the leading dollar sign) in a pair of curly braces, as in
${filename}X
Built-in Integer Arithmetic
$((expression))
If the variable is not defined or contains a NULL string, its value is
assumed to be zero.
The Single Quote
As we mentioned, all special characters are ignored by the shell if they
appear inside single quotes.
The Double Quote
Double quotes work similarly to single quotes, except that they're not
as restrictive. Whereas the single quotes
tell the shell to ignore all enclosed characters,
double quotes say to ignore most. In particular, the following three
characters are not ignored inside double quotes:
The Back Quote
Its purpose is not to protect characters from the shell but to tell the
shell to execute the enclosed
command and to insert the standard output from the command at that
point on the command line.
The general format for using back quotes is
`command`
A few important points here. First, spaces are not permitted on either
side of the equals sign.
Second, unlike most other programming languages, the shell has no
concept whatsoever of data types.
Because the shell has no concept of data types, variables are not
declared before they're used;
Displaying the Values of Variables
The $ character is a special character to the shell. If a valid variable
name follows the $,
the shell takes this as an indication that the value stored
inside that variable is to be substituted at that point.
(Remember, the shell performs variable substitution before it executes
the command )
The Null Value
A variable that contains no value is said to contain the null value.
It is the default case for variables that you never store values in.
When the shell performs its variable substitution, any values that are
null are completely removed
from the command line, without a trace:
The ${variable} Construct
The shell thinks filenameX is the full name of the variable because
it's composed entirely of valid variable name characters.
To avoid this problem, you can delimit the end of the variable name by
enclosing the entire name
(but not the leading dollar sign) in a pair of curly braces, as in
${filename}X
Built-in Integer Arithmetic
$((expression))
If the variable is not defined or contains a NULL string, its value is
assumed to be zero.
The Single Quote
As we mentioned, all special characters are ignored by the shell if they
appear inside single quotes.
The Double Quote
Double quotes work similarly to single quotes, except that they're not
as restrictive. Whereas the single quotes
tell the shell to ignore all enclosed characters,
double quotes say to ignore most. In particular, the following three
characters are not ignored inside double quotes:
The Back Quote
Its purpose is not to protect characters from the shell but to tell the
shell to execute the enclosed
command and to insert the standard output from the command at that
point on the command line.
The general format for using back quotes is
`command`