A parameter is an identifier, one or more digits, or any of the following characters:
* @ # ? - $ !
A variable (a parameter denoted by an identifier) has a value and zero or more attributes. Variables can be assigned values and attributes with the built-in typeset command. The attributes supported by the POSIX shell are described under typeset. Exported parameters pass their values and attributes to the environment.
The POSIX shell supports one-dimensional arrays. An element of an array variable is referenced by a subscript. A subscript is denoted by a left square bracket [, followed by an arithmetic expression (see the section “Arithmetic evaluation”), followed by a right square bracket ].
To assign values to an array, you can use the built-in set command as follows:
set -A name value...
The value of all subscripts must be in the permissible range. Arrays need not be declared; any reference to a variable with a valid subscript is legal, and an array will be created if necessary. Referencing an array without a subscript is equivalent to referencing element zero.
name=value[ name=value]...
The value of a variable may be assigned in this form. If the integer attribute is set for a variable, its value can be used for arithmetic evaluations. Positional parameters (denoted by a number) may be assigned values with the built-in set command. Parameter $0 is set from argument zero when the shell is invoked.
${parameter}
The dollar sign $ is used to introduce substitutable parameters. The POSIX shell reads all the characters from ${ to the matching } as part of the same word even if it contains braces or metacharacters. The value, if any, of the parameter is substituted. The braces are required when parameter is followed by a letter, digit, or underscore that is not to be interpreted as part of its name, or when a variable is subscripted. If parameter is one or more digits, then it is a positional parameter. A positional parameter with more than one digit must be enclosed in braces.
If parameter is defined by the character * or @, then all the positional parameters, starting with $1, are substituted. The first character of the IFS variable is used as the field separator (see below). If an array identifier with the subscript * or @ is used, the value for each of the elements is substituted (separated by the same field separator character).
${#parameter}
If parameter is * or @, the number of positional parameters is substituted. Otherwise, the length of the value of parameter is substituted.
${#identifier[*]}
The number of elements in the array identifier is substituted.
${parameter:-word}
If parameter is set and is non-null, its value is substituted; otherwise, word is substituted.
${parameter:=word}
If parameter is not set or is null, then it is set to word. The value of the parameter is then substituted.
Positional parameters may not be assigned values in this way.
${parameter:?word}
If parameter is set and is non-null, its value is substituted; otherwise, word is printed and the shell exits. If word is omitted, a standard message is printed.
${parameter:+word}
If parameter is set and is non-null, word is substituted; otherwise, the null string is substituted.
${parameter#pattern}
${parameter##pattern}
If the POSIX shell pattern matches the beginning of the value of parameter, then the value of this substitution is the value of parameter with the matched portion deleted; otherwise, the value of parameter is substituted. In the first form, the smallest matching pattern is deleted; in the second form, the largest matching pattern is deleted.
${parameter%pattern}
${parameter%%pattern}
If the POSIX shell pattern matches the end of the value of parameter, then the value of this substitution is the value of parameter with the matched portion deleted; otherwise, the value of parameter is substituted. In the first form, the smallest matching pattern is deleted; in the second form, the largest matching pattern is deleted.
In the previous 8 substitutions, word is not evaluated unless it is to be used as the substituted string.
Example
pwd is only executed if dir is not set or is equal to the null string.
echo ${dir:-$(pwd)}
If the colon is omitted from the above expressions, the POSIX shell only checks whether or not parameter is set; it does not check whether it is equal to the null string.
The following parameters are automatically set by the POSIX shell:
# | The number of positional parameters (in decimal). |
- | All options supplied to the POSIX shell on invocation or by the set command. |
? | The exit status returned by the last executed command. |
$ | The process ID of the current POSIX shell. |
_ | Initially, the value of _ (underscore) is an absolute path name of the POSIX shell or script being executed as passed in the environment. Thereafter, it is always assigned the last argument of the previous command. This parameter is not set for commands which run asynchronously. The underscore parameter is also used to hold the name of the matching MAIL file when checking for mail. Example The tail command uses $_ to access the last file of the preceding cat command.
|
! | The process number of the last command invoked as a background process. |
ERRNO | The value of errno as set by the most recently failed system call. This value is systemdependent and is intended for debugging purposes. |
LINENO | The line number of the current line within the script or function being executed. |
OLDPWD | The previous working directory set by the cd command. |
OPTARG | The value of the last option argument processed by the built-in getopts command. |
OPTIND | The index of the last option argument processed by the built-in getopts command. |
PPID | The process number of the parent of the current POSIX shell. |
PWD | The present working directory set by the cd command. |
RANDOM | Each time this variable is referenced, a random integer, uniformly distributed between 0 and 32767, is generated. You can initialize the sequence of random numbers by assigning a numeric value to RANDOM. |
REPLY | This variable is set by the select statement and by the built-in read command when no arguments are supplied. |
SECONDS | Each time this variable is referenced, the number of seconds since the invocation of the POSIX shell is returned. If this variable is assigned a value, the value returned upon reference will be the value that was assigned plus the number of seconds since the assignment. |
The following variables are used by the POSIX shell:
CDPATH | The search path for the cd command. |
COLUMNS | If this variable is set, then its value is used to define the width of the editing window when the POSIX shell is in edit mode as well as for outputting the select list. This variable is of use when the POSIX shell is accessed via rlogin. |
EDITOR | If the value of this variable ends with vi and if the variable VISUAL is not set then the corresponding option is set (see set). This variable is of use when the POSIX shell is accessed via rlogin. |
ENV | If this variable is set then it contains the path name of the procedure which is executed when the POSIX shell is called. This procedure is mostly used for function and alias definitions. The value of this variable is used to perform parameter substitution for file name generation. |
FCEDIT | The name of the standard editor for the built-in fc command. The only value which is currently permissible for this variable is edt. |
FPATH | The search path for function definitions. This path is used when a function having the attribute -u is accessed and no command is found. If an executable file is found then it is read and executed in the current environment. |
HISTFILE | If this variable is set when the POSIX shell is invoked, its value is the path name of the file that will be used to store the command history (see the section “Command re-entry”). |
HISTSIZE | If this variable is set when the POSIX shell is invoked, the shell will remember the commands you enter (command history). The number of previously entered commands that are accessible by this shell will be greater than or equal to the given number. The default is 128. |
HOME | The default argument (home directory) for the cd command. |
IFS | The POSIX shell’s internal field separator, used to separate command words which result from command or parameter substitution and to separate words with the built-in read command. The value of IFS is normally set to space, tab, and newline. The first character of the IFS variable is used to separate arguments in "$*" substitutions (see the section “Quoting metacharacters”). |
IO_CONVERSION | If this variable is set to YES, and if POSIX commands (e.g. awk, cat, grep etc.) are being used to access files in a (mounted) ASCII file system, then conversion is performed automatically. |
LINES | If this variable is set, its value is to determine the column length for printing select lists. select lists will print vertically until about two-thirds of LINES lines are filled. |
MAIL | If this variable is set to the name of a mail file and the MAILPATH variable is not set, the shell informs you of the arrival of mail in the specified file. |
MAILCHECK | The value of this variable specifies how often (in seconds) the POSIX shell should check for changes in the modification time of any of the files specified by the MAILPATH or MAIL variables. The default value for MAILCHECK is 600 seconds. When the specified time has elapsed, the POSIX shell checks the mail files before issuing the next prompt. |
MAILPATH | A colon-separated list of file names. If this variable is set, the shell informs you of any modifications to the specified files that have occurred within the last MAILCHECK seconds. Each file name may be followed in the list by a question mark and a message that is to be printed. The message will undergo parameter substitution, with the variable $_ defined as the name of the file that has changed. The default message is: "you have mail in $_". |
PATH | The search path for commands (see Execution). You may not change the value of this variable if you are using a restricted POSIX shell. |
PS1 | The value of this variable is expanded for parameter substitution in order to define the prompt used by the POSIX shell. The default value is “$ ” or, in the case of privileged users, “# ”. The use of an exclamation mark ! in the prompt is replaced by the command number (see the section “Command re-entry”). |
PS2 | Secondary prompt string displayed by the POSIX shell when further input is expected after a newline character. The default is “> ”. |
PS3 | Prompt string used within a select loop to prompt for the desired number. The default is “#? ”. |
PS4 | The value of this variable is expanded for parameter substitution and precedes each line of an execution trace. The default execution trace prompt is “+ ”. |
SHELL | The path name of the POSIX shell is stored in the environment. When called, the POSIX shell acts as a restricted shell if the pattern *r*sh* matches the file name component of the path name (see section “basename return non-directory portion of path name”). |
TMOUT | If TMOUT is set to a value greater than zero, the POSIX shell will automatically terminate if a command is not entered within the prescribed number of seconds after issuing the PS1 prompt. (Caution: the POSIX shell may have been compiled with a maximum bound for this value which cannot be exceeded). |
VISUAL | If the value of this variable ends with vi then the corresponding option is se (see section “Built-in POSIX commands”). This variable is of use when the POSIX shell is accessed via rlogin. |
The POSIX shell assigns default values to the following variables:
PATH, PS1, PS2, PS3, PS4, MAILCHECK, TMOUT and IFS |
The variables HOME, MAIL and SHELL are set by the command /START-POSIX-SHELL.