Definition
Command substitution is the shell mechanism that runs an embedded command, captures its standard output, and replaces the substitution construct with that output before the surrounding command is executed. The modern syntax is $(command); the older form using backticks remains supported but is harder to nest and to quote correctly. The trailing newlines of the captured output are stripped, but interior newlines are preserved unless word-splitting on whitespace is also in effect.
Command substitution is one of bash's expansion steps, performed in a defined order alongside parameter expansion, arithmetic expansion, and the others. It is the mechanism that lets one command's output become another command's argument — turning the shell from a sequencer of commands into a composer of expressions.
Why it matters
How it works
When bash encounters $(command), it forks a subshell, runs the inner command in that subshell with its standard output redirected to a pipe, reads everything written to the pipe, strips trailing newlines, and substitutes the captured string back into the outer command line. Word-splitting and pathname expansion then run on the result unless the substitution is inside double quotes. That last detail is the source of most quoting bugs: unquoted substitution splits the captured output on whitespace, which is rarely what you want when the output contains filenames with spaces or multi-line content.
The subshell isolation is both a feature and a limit. Any variable assigned inside the substituted command exists only in the subshell — the parent script never sees the new value. To pass information back from the substitution, you must use the captured output itself; assignment to a parent variable is impossible. Performance-conscious scripts avoid command substitution in tight loops because each invocation forks a new process; for arithmetic, the in-shell $(( ... )) is dramatically faster.