This section describes the following:
Variable substitution
Built-in shell variables
Other shell variables
Arrays (Korn shell only)
Discipline functions (ksh93 only)
ksh93 provides structured variables, such as pos.x
and pos.y
. To create either one, pos
must already exist, and braces must be used to retrieve their values. Names beginning with .sh
are reserved for use by ksh.
No spaces should be used in the following expressions. The colon (:
) is optional; if it's included, var must be nonnull as well as set.
var | Set each variable var to a value. |
| Use value of var; braces are optional if var is separated from the following text. They are required in ksh93 if a variable name contains periods. |
| Use var if set; otherwise, use value. |
| Use var if set; otherwise, use value and assign value to var. |
| Use var if set; otherwise, print value and exit (if not interactive). If value isn't supplied, print the phrase "parameter null or not set." |
| Use value if var is set; otherwise, use nothing. |
In the Korn shell:
| Use the length of var. |
| Use the number of positional parameters. |
| |
| Use value of var after removing pattern from the left. Remove the shortest matching piece. |
| Same as |
| Use value of var after removing pattern from the right. Remove the shortest matching piece. |
| Same as |
In ksh93:
| List of variables whose names begin with prefix. |
| |
| Starting at position pos (0-based) in variable var, extract len characters, or rest of string if no len. pos and len may be arithmetic expressions. |
| |
| Use value of var, with first match of pat replaced with repl. |
| Use value of var, with first match of pat deleted. |
| Use value of var, with every match of pat replaced with repl. |
| Use value of var, with match of pat replaced with repl. Match must occur at beginning of the value. |
| Use value of var, with match of pat replaced with repl. Match must occur at end of the value. |
In ksh93, indirect variables allow you to "alias" one variable name to affect the value of another. This is accomplished using typeset -n
:
$greet="hello, world"
Create initial variable $typeset -n friendly_message=greet
Set up alias $echo $friendly_message
Access old value through new name hello, world $friendly_message="don't panic"
Change the value $echo $greet
Old variable is changed don't panic
$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's 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 /, which 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 previously. 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. |
| First word; that is, command name. This will have the full path name if it was found via a PATH search. |
| Individual arguments on command line (positional parameters). The Bourne shell allows only nine parameters to be referenced directly (n = 1-9); the Korn shell allows n to be greater than 9 if specified as |
| All arguments on command line ( |
| All arguments on command line as one string ( |
| All arguments on command line, individually quoted ( |
The Korn shell automatically sets these additional variables:
ksh93 automatically sets these additional variables. Variables whose names contain "." must be enclosed in braces when referenced, e.g., ${.sh.edchar}
.
| The history number of the current command. |
The character(s) entered when processing a | |
| The position of the cursor in the most recent |
| Will be equal to ESCAPE if in a |
| The characters in the input buffer during a |
| The name of the variable running a discipline function. |
| The subscript of the variable running a discipline function. |
| The value of the variable inside the |
| The version of ksh93. |
The following variables 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
This list includes the type of value expected when defining these variables. Those that are specific to the Korn shell are marked as (K). Those that are specific to ksh93 are marked (K93).
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, 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 i of array name. i can be any arithmetic expression as described under let. The expression must return a value between 0 and 1023. |
| Use element 0 of array name. |
| Use all elements of array name. |
| |
| Use the number of elements in array name. |
|
ksh93 provides associative arrays, where the indices are strings instead of numbers (as in awk). In this case, [
and ]
act like double quotes. Associative arrays are created with typeset -A
. A special syntax allows assigning to multiple elements at once:
data=([joe]=30 [mary]=25)
The values would be retrieved as ${data[joe]}
and ${data[mary]}
.
Along with structured variables, ksh93 introduces discipline functions. These are special functions that are called whenever a variable's value is accessed or changed. For a shell variable named x
, you can define the following functions:
x.get | Called when |
x.set | Called when |
x.unset | Called when |
Within the discipline functions, special variables provide information about the variable being changed:
.sh.name | The name of the variable being changed. |
.sh.subscript | The subscript of the array element being changed. |
.sh.value | The value of the variable being assigned or returned. Changing it within the discipline function changes the value that is actually assigned or returned. |