← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 8 of 12

Command Substitution

Command substitution lets you run a command and drop its printed output directly into a variable or another command, as if you had typed the output yourself.

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

bash
#!/bin/bash
current_dir=$(pwd)
echo "Working directory: $current_dir"
file_count=$(ls | wc -l)
echo "Files here: $file_count"

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

bash
#!/bin/bash
today=`date +%Y-%m-%d`
echo "Today's date is $today"

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

bash
#!/bin/bash
year=$(date +%Y)
decade=$(echo "$(( year / 10 * 10 ))s")
echo "We are in the $decade"

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

bash
#!/bin/bash
echo "There are $(ls | wc -l) entries in $(pwd)"
Common Mistakes
  1. 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 $?).
  2. Forgetting to quote a command substitution, like files=$(ls) then for f in $files, which triggers word splitting on whitespace/newlines -- usually you want "$(...)" when treating it as a single string.
  3. 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 $(...).
Chapter Summary
  • $(command) runs command and 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).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.