Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Input and Output  ›  Lesson

Command Substitution: Capture Output

Command substitution 12 minCommand substitution, written $(command), runs a command and replaces itself with the command's output, minus trailing newlines.

Command Substitution: Capture Output

The very first version of backup.sh had the date typed into the archive name, and the "same way again?" test flagged it. The fix is to let the date command supply it. To do that, a script needs to take what a command prints and use it as a value.

The rule

$( ) runs a command and pastes its output in its place.

stamp="$(date +%F)"                          # 2026-09-25
archive="$dest/${name}-${stamp}.tar.gz"
echo "Today is $(date +%A)"                  # Today is Friday
count="$(find "$src" -type f | wc -l)"       # how many files

What happens, in order:

  1. Bash runs the command inside $( ) in a subshell, a temporary copy of itself.
  2. It collects everything the command prints on standard output.
  3. It removes trailing newlines, then puts the text where $( ) stood.

Three consequences:

  • Quote it like a variable: "$(...)". The result is split on spaces if left bare.
  • Anything the inner command changes about its shell, such as cd or a variable, vanishes when the subshell ends.
  • Error messages are not captured; they go to the terminal. Only standard output is pasted in.

You will also meet the old backquote form, `date +%F`. It does the same job, but it is harder to read and to nest. Prefer $( ), which nests cleanly: $(basename "$(pwd)").

A cousin, process substitution, looks similar but hands over a file name instead of text, so a command that expects files can read another command's output: diff <(ls dir1) <(ls dir2).

The shortcut

Evaluate from the inside out: run the innermost $( ) in your head, write its output in its place, trim trailing newlines, and repeat until no $( ) remains.

Expanding "$dest/${name}-$(date +%F).tar.gz"Step 1 of 4
  1. Find the innermost command

    date +%F, which prints the date as year-month-day.

Drills

Answer fast — trust the rule.

quick drill

On a Friday, what does echo "Today is $(date +%A)" print?

quick drill

What does x=$(printf 'hi\n\n\n'); echo "[$x]" print?

quick drill

What does echo $(( $(echo 3) * 2 )) print?

quick drill

A script in /home/asha runs where=$(cd /tmp; pwd), then pwd. What does the second pwd print?

Remember

  • $( ) runs a command and pastes its output, trailing newlines removed.
  • Evaluate inside out, and quote the result: "$(...)".
  • Trap: changes made inside $( ), such as cd, vanish with the subshell.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainStamp the archive with today's date

Make backup.sh name its archive with today's date, like notes-2026-09-25.tar.gz, without anyone typing the date. Show the lines, then explain what $( ) does, why you quote it, and why a cd inside $( ) does not move the script.