Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash command substitution

Reference for Bash command substitution: capture the output of a command with $(...), why it beats backticks, quoting, nesting, exit status and subshell effects.

Command substitution runs a command and replaces the whole expression with what the command printed to standard output. It is how you put the result of one program into a variable or into the arguments of another.

There are two spellings: $(command) and the older `command`. Prefer $(...); it nests cleanly and its quoting rules are simple. Trailing newlines are removed from the captured text.

Terminal
today=$(date +%F)
count=$(ls | wc -l)
echo "Files: $count"

Try it

Bash Capturing output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Exit status and subshell scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
$(command)
`command`
$(< file)          # fast read of a file, no external process
Part Description
command Any command, pipeline or list. It runs in a subshell and its standard output is captured.
$(< file) Bash shortcut that reads file directly; same result as $(cat file) without starting cat.

Returns: the output with all trailing newlines removed. Inside double quotes it is one word; unquoted it is subject to word splitting and globbing.

Errors / edge cases: NUL bytes in the output are dropped (with a warning). The exit status of an assignment var=$(cmd) is the exit status of cmd.

Usage notes

  • Always quote it: "$(cmd)". An unquoted result is split on spaces and expanded as a glob, which breaks on file names with spaces or stars.
  • Prefer $( ) over backticks. Backticks treat backslashes specially and require escaping when nested, and they are hard to read next to quotes.
  • It runs in a subshell. Variables set or cd performed inside are lost. If you need side effects, run the command normally and read from a file, or use process substitution.
  • Hidden exit status. local x=$(cmd) and export x=$(cmd) return the status of local/export, not of cmd. Declare first, then assign.
  • Trailing newlines vanish. If they matter, append a sentinel: out=$(cmd; echo x); out=${out%x}.
  • Big outputs are held in memory. For large data, stream through a pipe or a file instead.
  • Cost: each substitution forks a subshell. In tight loops, prefer built-ins such as parameter expansion over $(basename ...) or $(echo ... | cut ...).
  • Capturing both streams: out=$(cmd 2>&1); to separate them, redirect one of them to a temporary file.
  • set -e and substitutions: a failing $(...) inside an argument list does not stop the script, because only the final command's status counts.