Command substitution allows the output of a command to replace the command itself. The standard form of command substitution occurs when a command is enclosed as follows:
$(command)
or (deprecated)
`command`.
Bash performs command substitution by executing command in a subshell
environment and replacing the command substitution with the standard
output of the command, with any trailing newlines deleted.
Embedded newlines are not deleted, but they may be removed during
word splitting.
The command substitution $(cat file)
can be
replaced by the equivalent but faster $(< file)
.
With the old-style backquote form of substitution,
backslash retains its literal meaning except when followed by
‘$’, ‘`’, or ‘\’.
The first backquote not preceded by a backslash terminates the
command substitution.
When using the $(command)
form, all characters between
the parentheses make up the command; none are treated specially.
There is an alternate form of command substitution:
${c command; }
which executes command in the current execution environment and captures its output, again with trailing newlines removed.
The character c following the open brace must be a space, tab, newline, or ‘|’, and the close brace must be in a position where a reserved word may appear (i.e., preceded by a command terminator such as semicolon). Bash allows the close brace to be joined to the remaining characters in the word without being followed by a shell metacharacter as a reserved word would usually require.
Any side effects of command take effect immediately
in the current execution environment and persist in the current
environment after the command completes (e.g., the exit
builtin
exits the shell).
This type of command substitution superficially resembles executing an
unnamed shell function: local variables are created as when a shell
function is executing, and the return
builtin forces
command to complete;
however, the rest of the execution environment,
including the positional parameters, is shared with the caller.
If the first character following the open brace
is a ‘|’, the construct expands to the
value of the REPLY
shell variable after command executes,
without removing any trailing newlines,
and the standard output of command remains the same as in the
calling shell.
Bash creates REPLY
as an initially-unset local variable when
command executes, and restores REPLY
to the value it had
before the command substitution after command completes,
as with any local variable.
For example, this construct expands to ‘12345’, and leaves the
shell variable X
unchanged in the current execution environment:
${ local X=12345 ; echo $X; }
(not declaring X
as local would modify its value in the current
environment, as with normal shell function execution),
while this construct does not require any output to expand to
‘12345’:
${| REPLY=12345; }
and restores REPLY
to the value it had before the command substitution.
Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, Bash does not perform word splitting and filename expansion on the results.