-->
Page 20
${parameter:_word} | Use default values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. |
${parameter:=word} | Assign default values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way. |
${parameter:?word} | Display Error if null or unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. |
${parameter:+word} | Use Alternate Value. If parameter is null or unset, nothing is substituted; otherwise, the expansion of word is substituted. |
${#parameter} | The length in characters of the value of parameter is substituted. If parameter is * or @, the length substituted is the length of * expanded within double quotes. |
${parameter#word} ${parameter##word} |
The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the expansion is the value of parameter with the shortest matching pattern deleted (the # case) or the longest matching pattern deleted (the ## case). |
${parameter%word} ${parameter%%word} | The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the value of parameter, then the expansion is the value of parameter with the shortest matching pattern deleted (the % case) or the longest matching pattern deleted (the %% case). |
COMMAND SUBSTITUCION
Command substitution allows the output of a command to replace the command name.
There are two forms:
$(command )
or
`command'
performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
When the old_style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the old form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.
ARITHMETIC EXPANSION
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. There are two formats for arithmetic expansion:
$[expression] $((expression))
The expression is treated as if it were within double quotes, but a double quote inside the braces or parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.
The evaluation is performed according to the rules listed under "Arithmetic Evaluation," later in this section. If expression is invalid, bash prints a message indicating failure and no substitution occurs.
PROCESS SUBSTITUTION
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or output connected to a FIFO or some file in /
Page 21
dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.
On systems that support it, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.
WORD SPLITTING
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters. If the value of IFS is exactly <space><tab><newline>, the default, then any sequence of IFS characters serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs. IFS cannot be unset.
Explicit null arguments ("" or `') are retained. Implicit null arguments, resulting from the expansion of parameters that have no values, are removed.
Note that if no expansion occurs, no splitting is performed.
PATHNAME EXPANSION
After word splitting, unless the _f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern and replaced with an alphabetically sorted list of pathnames matching the pattern. If no matching pathnames are found, and the shell variable allow_null_glob_expansion is unset, the word is left unchanged. If the variable is set, and no matches are found, the word is removed. When a pattern is used for pathname generation, the character (.) at the start of a name or immediately following a slash must be matched explicitly, unless the shell variable glob_dot_filenames is set. The slash character must always be matched explicitly. In other cases, the (.) character is not treated specially.
The special pattern characters have the following meanings:
* | Matches any string, including the null string. |
? | Matches any single character. |
[...] | Matches any one of the enclosed characters. A pair of characters separated by a minus sign denotes a range; any character lexically between those two characters, inclusive, is matched. If the first character following the [ is a ! or a ^, then any character not enclosed is matched. A _ or ] may be matched by including it as the first or last character in the set. |
QUOTE REMOVAL
After the preceding expansions, all unquoted occurrences of the characters \, `, and " are removed.
REDIRECTIONBefore a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment. The following redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.
In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).