Skip to main content

How to Save a Command's Output in a Variable in the Terminal

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

today=$(date -u +%Y-%m-%d) runs date and keeps what it prints in a variable named today. PowerShell does the same with a plain assignment: $today = Get-Date -AsUTC -Format 'yyyy-MM-dd'.

The Bash manual calls this command substitution: Command substitution allows the output of a command to replace the command itself. zsh works the same way. PowerShell needs no special syntax, because the output of a command can go straight into a variable.

Syntax​

In zsh and bash:

Syntax
<name>=$(<command>)
echo "$<name>"
PartWhat it does
<name>The variable to fill. No spaces on either side of =
$(<command>)Runs <command> and puts what it prints in its place
"$<name>"Reads the variable. Keep the double quotes

The Bash manual: Bash performs command substitution by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

In PowerShell:

Syntax
$<name> = <command>
PartWhat it does
$<name>The variable to fill
<command>Any command or pipeline. What it outputs goes into the variable

Microsoft's reference: Because the assignment operator = has a lower precedence than the pipeline operator |, parentheses aren't required to assign the result of a command pipeline to a variable.

The Example​

The example saves today's date, in UTC, in a variable and uses it in a line of text. Then it saves the list of files in a folder that holds two empty files, notes.txt and plan.txt.

Step 1: Save a Command's Output​

Terminal
today=$(date -u +%Y-%m-%d)
echo "Report for $today"
Output
Report for 2026-11-26

date -u +%Y-%m-%d prints today's date in UTC as the year, the month and the day, and $(...) puts that text into today. Your output shows the day you run it.

Step 2: Save Output That Has Several Lines​

Run these in the folder with the two files:

Terminal
files=$(ls)
echo "$files"
Output
notes.txt
plan.txt

In double quotes, the variable keeps its line break. Without the quotes, zsh and bash differ:

Terminal
echo $files
Output (zsh)
notes.txt
plan.txt
Output (bash)
notes.txt plan.txt

bash split the unquoted variable into words, and echo printed them with spaces between. The Bash manual: The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. zsh splits unquoted variables only when its SH_WORD_SPLIT option is on: Causes field splitting to be performed on unquoted parameter expansions. The zsh manual marks that option as on by default only when zsh emulates ksh or sh.

macOS-style terminal window titled Terminal — zsh showing today saved with date and printed in Report for 2026-11-26, then files saved with ls and printed on two lines, both with and without double quotes

macOS-style terminal window titled Terminal — bash showing the same commands, where echo with double quotes prints notes.txt and plan.txt on two lines and echo without quotes prints them on one line

Common Mistakes​

MistakeWhat happensDo this instead
Spaces around = in zsh or bashtoday = $(date -u +%Y-%m-%d) runs a command named today: zsh reported command not found: today, and bash today: command not foundWrite today=$(...) with no spaces
Leaving the variable unquoted in bashThe lines are joined: echo $files printed notes.txt plan.txtQuote it: echo "$files"
Expecting error messages in the variable$(...) keeps only the standard output: missing=$(ls /does-not-exist) printed the error in the terminal and left missing emptyAdd 2>&1 inside the parentheses to keep the errors too. On a Mac, missing then held ls: /does-not-exist: No such file or directory

FAQ​

Can I use backticks instead?​

They work: today=`date -u +%Y-%m-%d` gave the same date. But the Bash manual lists the backtick form as deprecated, next to the standard $(command) form.

Does the variable keep the last line break?​

No, trailing line breaks are dropped: text=$(printf "done\n\n\n") stored just done, and echo "[$text]" printed [done]. The zsh manual says the same: the command is replaced with its standard output, with any trailing newlines deleted.

Conclusion​

In zsh and bash, name=$(command) saves what a command prints in a variable. Read it back in double quotes, "$name", to keep its line breaks. In PowerShell, $name = command does the same, and output with several items becomes an array.

Additional Resources​