Parameters are shell variables; they can be assigned values and their values can be accessed using a parameter substitution. A parameter name is either one of the special single punctuation or digit character parameters described below, or a letter followed by zero or more letters or digits (`_' counts as a letter). The later form can be treated as arrays by appending an array index of the form:
[expr] where expr is an arithmetic expression. Array indices are currently limited to the range 0 through 1023, inclusive. Parameter substitutions take the form $name, ${name} or ${name[expr]}, where name is a parameter name. If substitution is performed on a parameter (or an array parameter element) that is not set, a null string is substituted unless the nounset option (set -o nounset or set -u) is set, in which case an error occurs.Parameters can be assigned values in a number of ways. First, the shell implicitly sets some parameters like #, PWD, etc.; this is the only way the special single character parameters are set. Second, parameters are imported from the shell's environment at startup. Third, parameters can be assigned values on the command line, for example, ` FOO=bar' sets the parameter FOO to bar; multiple parameter assignments can be given on a single command line and they can be followed by a simple-command, in which case the assignments are in effect only for the duration of the command (such assignments are also exported, see below for implications of this). Note that both the parameter name and the = must be unquoted for the shell to recognize a parameter assignment. The fourth way of setting a parameter is with the export, readonly and typeset commands; see their descriptions in the Command Execution section. Fifth, for and select loops set parameters as well as the getopts, read and set -A commands. Lastly, parameters can be assigned values using assignment operators inside arithmetic expressions (see Arithmetic Expressions below) or using the ${name=value} form of parameter substitution (see below).
Parameters with the export attribute (set using the export or typeset -x commands, or by parameter assignments followed by simple commands) are put in the environment (see environ(7)) of commands run by the shell as name=value pairs. The order in which parameters appear in the environment of a command is unspecified. When the shell starts up, it extracts parameters and their values from its environment and automatically sets the export attribute for those parameters.
Modifiers can be applied to the ${name} form of parameter substitution:
${name:-word}
if name is set and not null, it is substituted, otherwise word is substituted.
${name:+word}
if name is set and not null, word is substituted, otherwise nothing is substituted.
${name:=word}
if name is set and not null, it is substituted, otherwise it is assigned word and the resulting value of name is substituted.
${name:?word}
if name is set and not null, it is substituted, otherwise word is printed on standard error (preceded by name:) and an error occurs (normally causing termination of a shell script, function or .-script). If word is omitted the string `parameter null or not set' is used instead.
In the above modifiers, the : can be omitted, in which case the conditions only depend on name being set (as opposed to set and not null). If word is needed, parameter, command, arithmetic and tilde substitution are performed on it; if word is not needed, it is not evaluated.
The following forms of parameter substitution can also be used:
${#name}
The number of positional parameters if name is *, @ or is not specified, or the length of the string value of parameter name.
${#name[*]}, ${#name[@]}
The number of elements in the array name.
${name#pattern}, ${name##pattern}
If pattern matches the beginning of the value of parameter name, the matched text is deleted from the result of substitution. A single # results in the shortest match, two #'s results in the longest match.
${name%pattern}, ${name%%pattern}
Like ${..#..} substitution, but it deletes from the end of the value.
The following special parameters are implicitly set by the shell and cannot be set directly using assignments:
!
Process id of the last background process started. If no background processes have been started, the parameter is not set.
#
The number of positional parameters (i.e., $1, $2, etc.).
$
The process ID of the shell, or the PID of the original shell if it is a subshell.
-
The concatenation of the current single letter options (see set command below for list of options).
?
The exit status of the last non-asynchronous command executed. If the last command was killed by a signal, $? is set to 128 plus the signal number.
0
The name the shell was invoked with (i.e., argv[0]), or the command-name if it was invoked with the -c option and the command-name was supplied, or the file argument, if it was supplied. If the posix option is not set, $0 is the name of the current function or script.
1 ... 9
The first nine positional parameters that were supplied to the shell, function or .-script. Further positional parameters may be accessed using ${number}.
*
All positional parameters (except parameter 0), i.e., $1 $2 $3.... If used outside of double quotes, parameters are separate words (which are subjected to word splitting); if used within double quotes, parameters are separated by the first character of the IFS parameter (or the empty string if IFS is null).
@
Same as $*, unless it is used inside double quotes, in which case a separate word is generated for each positional parameter - if there are no positional parameters, no word is generated ("$@" can be used to access arguments, verbatim, without losing null arguments or splitting arguments with spaces).
The following parameters are set and/or used by the shell:
_ (underscore)
When an external command is executed by the shell, this parameter is set in the environment of the new process to the path of the executed command. In interactive use, this parameter is also set in the parent shell to the last word of the previous command. When MAILPATH messages are evaluated, this parameter contains the name of the file that changed (see MAILPATH parameter below).
CDPATH
Search path for the cd built-in command. Works the same way as PATH for those directories not beginning with / in cd commands. Note that if CDPATH is set and does not contain . nor an empty path, the current directory is not searched.
COLUMNS
Set to the number of columns on the terminal or window. Currently set to the cols value as reported by stty(1) if that value is non-zero. This parameter is used by the interactive line editing modes, and by select, set -o and kill -l commands to format information in columns.
EDITOR
If the VISUAL parameter is not set, this parameter controls the command line editing mode for interactive shells. See VISUAL parameter below for how this works.
ENV
If this parameter is found to be set after any profile files are executed, the expanded value is used as a shell start-up file. It typically contains function and alias definitions.
ERRNO
Integer value of the shell's errno variable — indicates the reason the last system call failed.
Not implemented yet.
EXECSHELL
If set, this parameter is assumed to contain the shell that is to be used to execute commands that execve(2) fails to execute and which do not start with a ` #! shell' sequence.
FCEDIT
The editor used by the fc command (see below).
FPATH
Like PATH, but used when an undefined function is executed to locate the file defining the function. It is also searched when a command can't be found using PATH. See Functions below for more information.
HISTFILE
The name of the file used to store history. When assigned to, history is loaded from the specified file. Also, several invocations of the shell running on the same machine will share history if their
HISTFILE parameters all point at the same file.
NOTE: if HISTFILE isn't set, no history file is used. This is different from the original Korn shell, which uses
$HOME/.sh_history; in future, pdksh may also use a default history file.
HISTSIZE
The number of commands normally stored for history, default 128.
HOME
The default directory for the cd command and the value substituted for an unqualified ~ (see Tilde Expansion below).
IFS
Internal field separator, used during substitution and by the
read command, to split values into distinct arguments; normally set to space, tab and newline. See Substitution above for details.
Note: this parameter is not imported from the environment when the shell is started.
KSH_VERSION
The version of shell and the date the version was created (readonly). See also the version commands in Emacs Editing Mode and Vi Editing Mode sections, below.
LINENO
The line number of the function or shell script that is currently being executed.
LINES
Set to the number of lines on the terminal or window.
Not implemented yet.
MAIL
If set, the user will be informed of the arrival of mail in the named file. This parameter is ignored if the MAILPATH parameter is set.
MAILCHECK
How often, in seconds, the shell will check for mail in the file(s) specified by MAIL or MAILPATH. If 0, the shell checks before each prompt. The default is 600 (10 minutes).
MAILPATH
A list of files to be checked for mail. The list is colon separated, and each file may be followed by a ? and a message to be printed if new mail has arrived. Command, parameter and arithmetic substitution is performed on the message, and, during substitution, the parameter $_ contains the name of the file. The default message is you have mail in $_.
OLDPWD
The previous working directory. Unset if cd has not successfully changed directories since the shell started, or if the shell doesn't know where it is.
OPTARG
When using getopts, it contains the argument for a parsed option, if it requires one.
OPTIND
The index of the last argument processed when using getopts. Assigning 1 to this parameter causes getopts to process arguments from the beginning the next time it is invoked.
PATH
A colon separated list of directories that are searched when looking for commands and .'d files. An empty string resulting from a leading or trailing colon, or two adjacent colons is treated as a `.', the current directory.
POSIXLY_CORRECT
If set, this parameter causes the posix option to be enabled. See POSIX Mode below.
PPID
The process ID of the shell's parent (readonly).
PS1
PS1 is the primary prompt for interactive shells. Parameter, command and arithmetic substitutions are performed, and ! is replaced with the current command number (see fc command below). A literal ! can be put in the prompt by placing !! in PS1. Note that since the command line editors try to figure out how long the prompt is (so they know how far it is to edge of the screen), escape codes in the prompt tend to mess things up. You can tell the shell not to count certain sequences (such as escape codes) by prefixing your prompt with a non-printing character (such as control-A) followed by a carriage return and then delimiting the escape codes with this non-printing character. If you don't have any non-printing characters, you're out of luck... BTW, don't blame me for this hack; it's in the original ksh. Default is ` $ ' for non-root users, `# ' for root..
PS2
Secondary prompt string, by default `> ', used when more input is needed to complete a command.
PS3
Prompt used by select statement when reading a menu selection. Default is ` #? '.
PS4
Used to prefix commands that are printed during execution tracing (see set -x command below). Parameter, command and arithmetic substitutions are performed before it is printed. Default is ` + '.
PWD
The current working directory. Maybe unset or null if shell doesn't know where it is.
RANDOM
A simple random number generator. Every time RANDOM is referenced, it is assigned the next number in a random number series. The point in the series can be set by assigning a number to RANDOM (see rand(3)).
REPLY
Default parameter for the read command if no names are given. Also used in select loops to store the value that is read from standard input.
SECONDS
The number of seconds since the shell started or, if the parameter has been assigned an integer value, the number of seconds since the assignment plus the value that was assigned.
TMOUT
If set to a positive integer in an interactive shell, it specifies the maximum number of seconds the shell will wait for input after printing the primary prompt ( PS1). If the time is exceeded, the shell exits.
TMPDIR
The directory shell temporary files are created in. If this parameter is not set, or does not contain the absolute path of a writable directory, temporary files are created in /tmp.
VISUAL
If set, this parameter controls the command line editing mode for interactive shells. If the last component of the path specified in this parameter contains the string vi, emacs or gmacs, the vi, emacs or gmacs (Gosling emacs) editing mode is enabled, respectively.