Capture Command Output in Bash
Assign a command's output to a variable with $( ).
Use now=$(date +%s) to store the command output in a variable.
01Command
Command
$ now=$(date +%s); printf "timestamp: %s\n" "$now"02What it does
Command substitution runs a command and substitutes its output, letting scripts build values from other tools. The $( ) form nests cleanly and is preferred over backticks because quoting behaves predictably.
03Risk
!Warning: Always quote the substitution when assigning paths, or word splitting and globbing will corrupt the value.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- bash.1.en.html manual pageEvidence: The bash manual page documents command substitution.
Last verified: 2026-10-13 · Category: Developer