$linuxjunkies
>

command substitution

also: command expansion, $(), backticks

Command substitution is a shell feature that executes a command and replaces it with its output, allowing you to use a command's result as input to another command or variable assignment.

Command substitution lets you embed the output of one command into another command or assign it to a variable. The shell executes the inner command, captures its output, and substitutes it in place of the command itself.

There are two syntaxes: the modern $(command) form and the older backtick form `command`. The modern syntax is preferred because it's easier to read, nests better, and escapes more predictably.

Example: echo "Today is $(date +%A)" runs the date command and embeds its output in the echo statement, producing something like Today is Monday. Another example: files=$(ls *.txt) stores the list of text files in the files variable for later use.

Related terms