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.
Variables
Assign with name=value: no spaces around =. Read with $name or ${name}.
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$variablesand$(commands). - Single quotes
'...'are completely literal. - Unquoted variables are split on spaces and glob-expanded, a classic source of bugs. Quote your variables:
"$file".
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?
Your home directoryThe literal text $HOMENothingAn error
Show answer
Answer: The literal text $HOME