Quoting and expansion
Before Bash runs a command line, it expands it: it replaces variables, runs $( ), expands * into file names, and splits the result into words. Quotes control which of those steps happen. Most shell-script bugs, including some that have deleted entire servers, come from a missing pair of quotes.
The three kinds of quotes
| Quotes | What happens inside | Example |
|---|---|---|
"double" |
Variables and $( ) are expanded; spaces and * are kept as they are |
"Hello $name" |
'single' |
Nothing is expanded; every character is literal | 'Costs $5' |
| none | Everything is expanded, then split into words and matched against file names | $name |
name="Asha"
echo "Hello $name, you are logged in as $(whoami)"
echo 'Hello $name, you are logged in as $(whoami)'
echo "A literal dollar: \$5 and a quote: \""
echo 'It'\''s tricky to put a single quote in single quotes'
echo "It's easy inside double quotes"Hello Asha, you are logged in as you Hello $name, you are logged in as $(whoami) A literal dollar: $5 and a quote: " It's tricky to put a single quote in single quotes It's easy inside double quotes
Use single quotes for text that must stay exactly as written (regular expressions, awk programs, JSON). Use double quotes around anything that contains a variable.
Word splitting: why you quote variables
Without quotes, a variable's value is split on spaces into separate words:
touch -d "2026-11-22 09:00" "my report.txt"
file="my report.txt"
ls -l $file
echo "---"
ls -l "$file"ls: cannot access 'my': No such file or directory ls: cannot access 'report.txt': No such file or directory --- -rw-r--r-- 1 you you 0 Nov 22 09:00 'my report.txt'
Unquoted, ls received two arguments, my and report.txt, neither of which exists. Quoted, it got one. Now imagine a cleanup script:
dir="/tmp/old builds"
# rm -rf $dir ← would delete /tmp/old AND ./builds
# rm -rf "$dir" ← deletes exactly "/tmp/old builds"⚠️ Rule of thumb: always write
"$var", unless you have a specific reason not to. ShellCheck warns about every unquoted variable (SC2086).
"$@" vs $*
The same splitting applies to arguments. "$@" keeps each argument whole; unquoted $@ or $* re-splits them:
#!/usr/bin/env bash
echo 'With "$@":'
for a in "$@"; do echo " [$a]"; done
echo 'With $*:'
# shellcheck disable=SC2048
for a in $*; do echo " [$a]"; donebash args.sh "New York" "San Jose"With "$@": [New York] [San Jose] With $*: [New] [York] [San] [Jose]
When a script passes its arguments on to another program, always use "$@".
Globs: file name patterns
Unquoted *, ? and [ ] are globs: Bash replaces them with matching file names before the command runs. The command never sees the *.
mkdir globs && cd globs
touch app.js app.test.js index.html notes.txt photo1.jpg photo2.jpg photo10.jpg
echo *.js
echo photo?.jpg
echo photo[0-9]*.jpg
echo *.{html,txt}
echo "*.js"
echo *.pdfapp.js app.test.js photo1.jpg photo2.jpg photo1.jpg photo10.jpg photo2.jpg index.html notes.txt *.js *.pdf
| Glob | Matches |
|---|---|
* |
Any characters (but not a leading ., so hidden files are skipped) |
? |
Exactly one character |
[abc], [0-9] |
One character from the set or range |
Notice the last line: if nothing matches, Bash leaves the pattern as it is. In a loop that would run once with the literal text *.pdf. Turn on shopt -s nullglob to get nothing instead:
cd globs
shopt -s nullglob
for f in *.pdf; do echo "found $f"; done
echo "pdf files: $(echo *.pdf | wc -w)"pdf files: 0
Brace expansion
Braces generate text, whether or not files exist. Great for creating many folders at once:
echo file{1..5}.txt
echo {a,b,c}-{x,y}
echo {01..12}
echo {10..0..5}
mkdir -p project/{src,tests,docs}
ls project
touch project/README.md
cp -v project/README.md{,.bak}file1.txt file2.txt file3.txt file4.txt file5.txt a-x a-y b-x b-y c-x c-y 01 02 03 04 05 06 07 08 09 10 11 12 10 5 0 docs src tests 'project/README.md' -> 'project/README.md.bak'
file.txt{,.bak} expands to file.txt file.txt.bak: a neat way to copy a file to a backup.
Tilde and parameter tricks
~ expands to your home folder (but only unquoted, at the start of a word):
echo ~
echo "~"
echo ~/projects/home/you ~ /home/you/projects
${ } can also transform a variable's value on the fly:
path="/var/log/nginx/access.log.gz"
echo "${#path}" # length
echo "${path##*/}" # remove everything up to the last / → file name
echo "${path%/*}" # remove the shortest /… from the end → folder
echo "${path%.gz}" # remove a suffix
echo "${path/log/LOG}" # replace the first match
echo "${path//log/LOG}" # replace every match
name="asha"
echo "${name^}" "${name^^}"28 access.log.gz /var/log/nginx /var/log/nginx/access.log /var/LOG/nginx/access.log.gz /var/LOG/nginx/access.LOG.gz Asha ASHA
| Syntax | Result |
|---|---|
${#var} |
Length |
${var#pattern} / ${var##pattern} |
Remove the shortest / longest match from the start |
${var%pattern} / ${var%%pattern} |
Remove the shortest / longest match from the end |
${var/old/new} / ${var//old/new} |
Replace the first / every match |
${var^} / ${var^^} / ${var,,} |
Uppercase first letter / all / lowercase all |
These run inside Bash, so they're much faster than calling basename, dirname or sed in a loop.
The order of expansion
Bash expands a line in this order: braces → tilde → variables, arithmetic and $( ) → word splitting → globs → quote removal. Double quotes switch off the last three steps for the text inside. That's the whole secret: quote anything that holds data, and only leave *, {a,b} and ~ unquoted when you want them expanded.
Seeing what Bash will run
When a line doesn't do what you expect, ask Bash to show you:
files="a b"
set -x
printf '[%s]\n' $files "$files" ~/*.none
set +x+ printf '[%s]\n' a b 'a b' '/home/you/*.none' [a] [b] [a b] [/home/you/*.none] + set +x
set -x prints each command after expansion, prefixed with +. You'll use it a lot in the Error handling and debugging lesson.
Practice
- Create files
jan report.csv,feb report.csvand print the line count of each with a loop over*.csv. It must work with the spaces. - Use brace expansion to create
logs/2026-01…logs/2026-12in one command. - Given
f=photo.final.jpeg, print just the extension (jpeg) and the name without it (photo.final) using${ }.
1.name=Asha. What does echo '$name' print?
2.Why does rm $file fail when file="my notes.txt"?
3.Which expands to backup.sql backup.sql.bak?
4.p=/srv/app/server.log. What is ${p##*/}?