Bash Tutorial · Bash Scripting · Lesson 14 of 30

Bash Variables and Quoting

Assign and use variables, understand single versus double quotes, and capture command output.

Create a free account to save this page, take notes and track your progress. Sign in

Variables

Assign with name=value: no spaces around =. Read with $name or ${name}.

Assigning and reading variables
app="shop-api"
version=3
echo "$app v$version"
echo "${app}_backup"
today=$(date -u -d 2026-01-15 +%F)
echo "release date: $today"

Output

shop-api v3
shop-api_backup
release date: 2026-01-15

Run this on your computer — see “Get Started” for setup.

Quoting

  • Double quotes "..." keep text together but still expand $variables and $(commands).
  • Single quotes '...' are completely literal.
  • Unquoted variables are split on spaces and glob-expanded, a classic source of bugs. Quote your variables: "$file".
Why quotes matter
file="annual report.txt"
touch "$file"
echo 'single: $file'
echo "double: $file"
ls "$file"
ls $file 2>&1 | sed 's/^ls: //' || true

Output

single: $file
double: annual report.txt
annual report.txt
cannot access 'annual': No such file or directory
cannot access 'report.txt': No such file or directory

Run this on your computer — see “Get Started” for setup.

Command substitution

$(command) runs a command and inserts its output.

Test yourself

What does echo '$HOME' print?

  1. Your home directory
  2. The literal text $HOME
  3. Nothing
  4. An error
Show answer

Answer: The literal text $HOME

Was this page helpful?