[Bash] Capture command output in a variable

Created on November 30, 2026

Use command substitution, $(command), to save a command's standard output in a variable.

result=$(printf 'hello\n')
printf '%s\n' "$result"
hello

The substitution strips trailing newlines from the captured output.

Bash Shell command