User-defined variables in the Bourne shell
The Bourne shell recognizes alphanumeric variables to which string values can be assigned.
Name=String
A name is a sequence of letters, digits, and underscores that begins with
an underscore or a letter. To use the value that you have assigned to a variable,
add a dollar sign ($
) to the beginning of its name. Thus,
the $Name variable yields the value specified by the String variable.
Note that no spaces are on either side of the equal sign (=
)
in an assignment statement. (Positional parameters cannot appear in an assignment
statement. You can put more than one assignment on a command
line, but remember that the shell performs the assignments from right to left.
If you enclose the String variable with double or single
quotation marks ("
or '
), the shell does
not treat blanks, tabs, semicolons, and newline characters within the string
as word delimiters, but it imbeds them literally in the string.
If you enclose the String variable with double quotation
marks ("
), the shell still recognizes variable names in the
string and performs variable substitution; that is, it replaces references
to positional parameters and other variable names that are prefaced by dollar
sign ($
) with their corresponding values, if any. The shell
also performs command substitution within strings that are enclosed in double
quotation marks.
'
), the shell does not substitute variables or commands
within the string. The following sequence illustrates this difference: You: num=875
number1="Add $num"
number2='Add $num'
echo $number1
System: Add 875
You: echo $number2
System: Add $num
$first
and $second
having
the same value: first='a string with embedded blanks'
second=$first
{ }
to
delimit the variable name from any string following. In particular, if the
character immediately following the name is a letter, digit, or underscore,
and the variable is not a positional parameter, then the braces are required: You: a='This is a'
echo "${a}n example"
System: This is an example
You: echo "$a test"
System: This is a test