Go to the first, previous, next, last section, table of contents.
Substitutions provide a way of including text in a word or wordlist by describing how the text is to be discovered, rather than exactly what text is to be used. All substitutions (with one exception mentioned under Command Substitution ) are introduced by a dollar sign ($). This is generally followed by some text enclosed between two grouping symbols, either braces, parentheses or brackets, with the interpretation of the content depending on which grouping symbols are used. As Variable Substitution is very common, it is permitted in simple cases to omit the grouping symbol (in this case braces) and the extent of the substitution is determined based on the first character as follows, rather than simply up to the closing brace.
If this character is an alphabetic or underscore character, a maximal sequence of alphanumeric characters is taken; if it is a digit, a maximal sequence of digits, possibly followed by an asterisk or an at sign (`*' or `@') is taken; if it is the single character name of a parameter, just that one character is taken; otherwise the dollar sign is ignored, and not treated specially at all.
Substitution only occurs when the leading dollar sign is unquoted, or quoted only with double quotes. In either case, the text that is substituted has the same quoting attribute as that leading dollar sign.
Any quotation symbols within the grouping symbols are local to the grouping. e.g. the first double quote within the grouping symbols starts a section of quoted text, and does not terminate a quoted section begun outside the grouping.
Parameters and Variables are named values which can be included in a word or wordlist. The basic differences between variables and parameters are that the value of a variable may be changed directly while a parameter has a specific meaning, and its value may only be changed indirectly if at all; and that parameters are named by single special characters whereas variables are named by strings of alpha-numerics. [ FIX-ME what about underscores ] For more information on what parameters are defined, and the general use of variables, see Parameters and Variables below.
The simplest way to included the value of a named value (i.e. a parameter or variable) into a word is to prefix the name with a dollar sign ($), thus
$fred
will be replaced by the value of the variable FRED and
$+
will be replaced by the value of the parameter `+'.
The more general method of including named values is with substitutions of the form
${<name><modifier>}
where <modifier> is optional and is either a default modifier or a list of value modifiers preceded by a colon (:).
A default modifier (with one exception) only affects the substitution if the name is not associated with any value or, possibly, if the value associated is empty. Default modifiers have the form
[:][-+=?]< string>
If the leading colon if given, then the modifier has effect if the value of the name is empty or non-existent, otherwise it only has effect if the value is non-existent.
The next character controls what the modifier does.
In all cases, the string is subject to further substitution if, and only if, it is to be used.
Value modifiers modify the value before it is substituted. The different modifiers are applied in sequence, and the resulting string is substituted. If the value is a `hard-array' value (see Variables below), then the modifiers are applied independently to each element of the array, and a list of resultant values is substituted.
The possible modifiers, with their actions, are:
/=+=/=2+=1/will produce the string "y+z+x".
Command substitution allows for the inclusion of the output of a command into a word. Command substitution is invoked by enclosing a command in parentheses, and prefixing with a dollar sign, thus
$(command)
invokes command substitution of the command command. To provide backward compatibility with earlier shells, command substitution can also be performed by enclosing the command between back quotes (grave accents) as
`command`
The effect of command substitution is to run the command with standard output collected by the shell rather than being sent where standard output would normally go. The shell then strips off any trailing newline characters (leaving any newlines in the middle of the text alone) and replaces the word by this output. "' FIX-ME where will command substitution fail
In the general case, a new process is forked to run the command and a pipe is created between that process and the shell for collection of the output. If the command is a simple builtin command, with no I/O redirection "' FIX-ME what about named parameters? and is one of those marked as non-forking, then the command is run without the overhead of forking.
As a special case to provide often used functionality, if the command is a simple command with no actual command words, and with redirection of standard input as the only I/O redirection, then the file that standard input was redirected from is read and used as though it were the output of the command. Thus
$(< afile )
has the same effect as
$( cat afile)
but without the overhead of forking and running
cat.
"' what about $(| commands) and $( commands | )
Arithmetic Substitution allows the value of an arithmetic expression to be included (in base 10) in a word list. Any text enclosed in brackets and preceded by a dollar sign is treated as an arithmetic expression (see Arithmetic) and the result is converted to base 10 and substituted. "' FIXME why no $[45+56:n6] for base six For example
echo $[i++]
will print the current value of I and increment its value.
Go to the first, previous, next, last section, table of contents.