Command Substitution
In this page:
Basic $(...) Substitution
$(command) executes command and is replaced by whatever that command printed to standard output, with any trailing newline removed. The result can be stored in a variable or used directly inline in another command.
Example: Basic $(...) Substitution
#!/bin/bash
current_dir=$(pwd)
echo "Working directory: $current_dir"
file_count=$(ls | wc -l)
echo "Files here: $file_count"
Login to try C/C++/Java/PHP code in the editor
Legacy Backtick Syntax
Backticks ` command are the older way to do command substitution and behave the same as $(...)` for simple cases. They are considered legacy because nesting them requires escaping the inner backticks with backslashes, which quickly becomes unreadable.
Warning: Prefer $(...) over backticks in new scripts -- it nests cleanly and is visually easier to match.
Example: Legacy Backtick Syntax
#!/bin/bash
today=`date +%Y-%m-%d`
echo "Today's date is $today"
Login to try C/C++/Java/PHP code in the editor
Nesting Command Substitutions
$(...) can be nested directly inside another $(...) without any special escaping, which is one of its biggest advantages over backticks. Bash resolves the innermost substitution first, then the outer one.
Example: Nesting Command Substitutions
#!/bin/bash
year=$(date +%Y)
decade=$(echo "$(( year / 10 * 10 ))s")
echo "We are in the $decade"
Login to try C/C++/Java/PHP code in the editor
Using Substitution Directly in Commands
Command substitution does not have to be assigned to a variable first -- it can be used inline as an argument to any other command, letting you compose commands together. Quoting the substitution with double quotes preserves internal spacing and prevents unwanted word splitting.
Example: Using Substitution Directly in Commands
#!/bin/bash
echo "There are $(ls | wc -l) entries in $(pwd)"
Login to try C/C++/Java/PHP code in the editor
- Thinking command substitution captures a command's exit status; it actually captures standard output text, and the exit status of the substitution is the exit status of the last command inside it (accessible separately via
$?). - Forgetting to quote a command substitution, like
files=$(ls)thenfor f in $files, which triggers word splitting on whitespace/newlines -- usually you want"$(...)"when treating it as a single string. - Assuming backticks and
$(...)are totally interchangeable in all cases; backticks are harder to nest and read (needing backslash-escaped inner backticks), so modern style prefers$(...).
$(command)runscommandand substitutes its standard output (trailing newlines stripped) into place.- Backticks `
command` do the same thing but are the older, legacy syntax that is harder to nest. $(...)nests cleanly, e.g.$(echo $(date +%Y)), while nested backticks require escaping.- Command substitution only captures stdout, not stderr, unless you explicitly redirect stderr into stdout first (
2>&1).
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: