This subsection describes the following:
Variable substitution
Built-in shell variables
Other shell variables
Arrays (Korn shell only)
No spaces should be used in the expressions below. The colon
(:
) is optional; if it's included, var
must be non-null as
well as set.
| Set each variable |
${ | Use value of |
${ | Use |
${ | Use |
${ | Use |
${ | Use |
In the Korn shell:
${# | Use the length of |
${#*} | Use the number of positional parameters. |
${#@} | Use the number of positional parameters. |
${ | Use value of |
${ | Same as # |
${ | Use value of |
${ | Same as % |
$u=up d=down blank=
Assign values to three variables (last is null). $echo ${u}root
Braces are needed here. uproot $echo ${u-$d}
Display value of u or d; since u is set, it is printed. up $echo ${tmp-`date`}
If tmp is not set, the date command is executed. Thu Feb 4 15:03:46 EST 1993 $echo ${blank="no data"}
blank is set, so it is printed (a blank line). $echo ${blank:="no data"}
blank is set but null, so the string is printed no data $echo $blank
blank now has a new value no data
tail='${PWD##*/}' Take the current directory name and remove the longest character string ending with /. This removes the leading pathname and leaves the tail.
Built-in variables are automatically set by the shell and are typically used inside shell scripts. Built-in variables can make use of the variable substitution patterns shown above. Note that the $ is not actually part of the variable name, although the variable is always referenced this way.
$# | Number of command-line arguments. |
$- | Options currently in effect (arguments supplied to sh or to set). |
$? | Exit value of last executed command. |
$$ | Process number of current process. |
$! | Process number of last background command. |
$0 | First word; that is, command name. |
$ | Individual arguments on command line (positional parameters).
The Bourne shell allows only nine parameters to be referenced
directly ( |
$* | All arguments on command line ("$1 $2..."). |
"$@" | All arguments on command line, individually quoted ("$1" "$2" ...). |
The Korn shell automatically sets these additional variables:
The variables below are not automatically set by the shell. They are typically used in your .profile file, where you can define them to suit your needs. Variables can be assigned values by issuing commands of the form:
$
variable
=value
The list below includes the type of value expected when defining these variables. Those that are specific to the Korn shell are marked as (K).
The Korn shell supports one-dimensional arrays of up to 1024 elements.
The first element is numbered 0.
An array name
can be initialized as follows:
set -A
name value0 value1 ...
where the specified values become elements of name
.
Declaring arrays is not required, however. Any valid reference to
a subscripted variable can create an array.
When referencing arrays, you can use the ${ ... } syntax. This isn't needed when referencing arrays inside (( )) (the form of let that does automatic quoting). Note that [ and ] are typed literally (i.e., they don't stand for optional syntax).
${ | Use element |
${ | Use all elements of array |
${ | Use element 0 of array |
${ | Use all elements in array |
${# | Use the number of elements in array |
${# | Use the number of elements in array |