Bash Command Substitution: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[bash#Subjects|bash]]
* [[bash#Subjects|bash]]
* [[Bash_Command_Line_Expansion#Command_Substitution_.24.28....29|Command Line Expansion]]


=Overview=
=Overview=
Line 15: Line 16:
</syntaxhighlight>
</syntaxhighlight>


Command substitution allows the output of a command or [[Bash_Functions#Returning_Values|function]] to replace the command or function itself.
Command substitution allows the stdout of a command or [[Bash_Functions#Returning_Values|function]] to replace the command or function itself. The command is executed in a [[Bash_Concepts#Subshell|subshell]] environment, and the stdout of the command, which would normally be sent to stdout of the executing shell, is used to replace the command. The output can be saved into a variable as follows:
 
The command is executed in a subshell environment, and the stdout of the command, which would normally be sent to stdout of the executing shell, is used to replace the command. The output can be saved into a variable as follows:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 24: Line 23:
</syntaxhighlight>
</syntaxhighlight>


The stdout is processed as follows: Any trailing newlines are deleted. Embedded newlines are not deleted, <font color=darkgray>but they may be removed during word splitting</font>. If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
The stdout is processed as follows: Any trailing newlines are deleted. Embedded newlines are not deleted, <font color=darkgray>but they may be removed during [[Bash_Command_Line_Expansion#Word_Splitting|word splitting]]</font>. If the substitution appears within double quotes, [[Bash_Command_Line_Expansion#Word_Splitting|word splitting]] and filename expansion are not performed on the results.
 
If stdout content is an empty string, the behavior is noop.
 
If the command produces additional stderr content, or only stderr content, the stderr content is redirected to the stderr of the invoking shell and it is not involved in command substitution.
 
It is possible to coalesce stdout and stderr in a single command substitution as follows. In this case, the stderr content is concatenated to the stdout content and command substitution is applied to the resulted string.
<syntaxhighlight lang="bash">
$(command 2>&1)
</syntaxhighlight>


=Special Cases=
=Special Cases=

Latest revision as of 02:12, 23 July 2020

Internal

Overview

Command substitution is invoked with:

$(command)

An equivalent, older and obsolete style is:

`command`

Command substitution allows the stdout of a command or function to replace the command or function itself. The command is executed in a subshell environment, and the stdout of the command, which would normally be sent to stdout of the executing shell, is used to replace the command. The output can be saved into a variable as follows:

local content
content=$(ps -ef)

The stdout is processed as follows: Any trailing newlines are deleted. Embedded newlines are not deleted, but they may be removed during word splitting. If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

If stdout content is an empty string, the behavior is noop.

If the command produces additional stderr content, or only stderr content, the stderr content is redirected to the stderr of the invoking shell and it is not involved in command substitution.

It is possible to coalesce stdout and stderr in a single command substitution as follows. In this case, the stderr content is concatenated to the stdout content and command substitution is applied to the resulted string.

$(command 2>&1)

Special Cases

Command substitution:

$(cat file)

can be replaced with the faster:

$(< file)