Capture Command Output in Bash

Assign a command's output to a variable with $( ).

macOSbashshellscriptingLow risk
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

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!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

Last verified: 2026-10-13 · Category: Developer