Korn Shell (ksh) Variables
In the Korn Shell (ksh), variables are used to store and manipulate data. Ksh supports both scalar and array variables.
Scalar Variables:
- Scalar variables hold a single value at a time.
- You can assign a value to a scalar variable using the equals sign (
=
). - Variable names are case-sensitive and consist of letters, digits, and underscores. They must start with a letter or an underscore.
Array Variables:
- Array variables can store multiple values using indices.
- You can assign values to specific indices of an array variable.
- To refer to the entire array, use
@
or*
.
Special Variables:
Ksh also has special variables that have predefined meanings. For example:
$0
: The name of the script or shell.$1
,$2
, etc.: Represent the positional parameters.$#
: The number of positional parameters.$?
: The exit status of the last command. A value of 0 usually indicates success, and non-zero values indicate an error.$$
: The process ID of the shell.$!
: The process ID of the last background command.
Input/Output/Errors:
$*
: All positional parameters as a single word (not recommended, use"$@"
instead).$@
: All positional parameters as separate words (preferred over$*
).$#
: The number of positional parameters.
Read-Only Variables:
- Some variables in ksh are read-only and provide information about the shell environment.
- Examples include
RANDOM
(generates a random number) andLINENO
(current line number in the script).