command substitution
Running a command inside $( ) and replacing it with that command's standard output, as in today=$(date).
In more detail
Trailing newlines are removed from the captured text. The command runs in a subshell, so variable changes inside it are lost. Put the substitution in double quotes to stop its output being split and globbed, and prefer $( ) over backticks because it nests cleanly.
Example
here=$(basename "$(pwd)")Related terms
- backtick — The ` character, which in shell scripts is the old way to write command substitution.
- subshell — A child copy of the current shell, created by ( ), by command substitution and by each stage of a pipe.
- double quotes — Quotes that keep text together as one word but still let dollar expansions and backticks work inside.
Taught in
- What Bash Does To A Line Before It Runs It — Bash Scripting & Command Line Automation
- Arrays, And Why Dollar At Is Not Dollar Star — Bash Scripting & Command Line Automation
- Redirection: Truncate, Append, Both Streams — Bash Scripting & Command Line Automation
- Shell Variables, The Environment And export — Bash Scripting & Command Line Automation
- Command Substitution And Subshells — Bash Scripting & Command Line Automation
- Arithmetic: Double Parentheses, let And bc — Bash Scripting & Command Line Automation
- Functions, local And declare — Bash Scripting & Command Line Automation
- set -e, set -u And pipefail: Promises And Traps — Bash Scripting & Command Line Automation
- Debugging: set -x, bash -n And Reading The Error — Bash Scripting & Command Line Automation
- ShellCheck — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation