TExcelTAD
Home › Linux & Bash › Bash Quoting

Bash Quoting

Quoting decides what Bash does with special characters such as spaces, $, * and \. Most shell bugs, from "my script breaks on file names with spaces" to accidental deletions, come down to quoting. The rules are short; learn them well.

Three kinds of quoting

Quoting Example What happens
Single quotes '$HOME *' Everything literal: no expansion at all
Double quotes "$HOME *" Variables, $( ) and $(( )) expand; spaces and * are kept literally
Backslash \$HOME Escapes the single next character
bash · try it yourself
item="coffee beans"
echo 'Single: $item $(echo hi) *'
echo "Double: $item $(echo hi) *"
echo Backslash: \$item \*
echo "Inside double quotes: \"quoted\" and \$item"
Single: $item $(echo hi) *
Double: coffee beans hi *
Backslash: $item *
Inside double quotes: "quoted" and $item

Word splitting: why "$var" matters

When a variable is used without double quotes, Bash splits its value on spaces into separate words and expands wildcards in it. Inside double quotes the value stays one word:

bash · try it yourself
file="annual report.txt"
touch "$file"
ls $file
echo "--- with quotes:"
ls "$file"
ls: cannot access 'annual': No such file or directory
ls: cannot access 'report.txt': No such file or directory
--- with quotes:
annual report.txt

Unquoted, ls got two arguments: annual and report.txt. With quotes it got one. A small helper shows exactly how many arguments a command receives:

bash · try it yourself
count_args() { echo "$# args:$(printf ' [%s]' "$@")"; }
title="The   Linux   Command Line"
count_args $title
count_args "$title"
count_args '$title'
4 args: [The] [Linux] [Command] [Line]
1 args: [The   Linux   Command Line]
1 args: [$title]

Note how the unquoted version also lost the extra spaces.

Rule of thumb: Always put variable and command-substitution expansions in double quotes, "$var" and "$(cmd)", unless you specifically want splitting. Tools like ShellCheck warn about every unquoted expansion.

Wildcards inside variables

Unquoted expansions also go through globbing:

bash · try it yourself
touch a.txt b.txt
pattern="*.txt"
echo $pattern
echo "$pattern"
a.txt b.txt
*.txt

"$@": passing arguments on safely

"$@" expands to all arguments, each as its own word, exactly as received. $* and unquoted $@ re-split them:

bash · try it yourself
show() { for arg in "$@"; do echo "  <$arg>"; done; }
forward_good() { show "$@"; }
forward_bad() { show $@; }
echo "good:"; forward_good "New York" "Rio de Janeiro"
echo "bad:";  forward_bad "New York" "Rio de Janeiro"
good:
  <New York>
  <Rio de Janeiro>
bad:
  <New>
  <York>
  <Rio>
  <de>
  <Janeiro>

Always write "$@" when passing arguments to another command.

Quotes inside quotes

You want Write
A double quote in double quotes "say \"hi\""
A single quote in single quotes 'it'\''s' (end, escaped quote, restart)
A single quote, easier "it's"
Escapes like \n and \t
#39;line1\nline2'
(ANSI-C quoting)
bash · try it yourself
echo "She said \"hello\""
echo 'It'\''s fine'
echo "It's also fine"
echo 
  

tab:\there\nnew line'
She said "hello"
It's fine
It's also fine
tab:	here
new line

Quoting and special characters

Characters with special meaning to Bash include: space, $ ' " \ * ? [ ] { } ( ) < > | & ; # ~ !. Quote or escape them when you mean them literally:

bash · try it yourself
echo "Total: 5 < 10 & 3 > 2; #1"
echo Total: 5 \< 10 \& 3 \> 2\; \#1
touch 'weird name (copy) [1].txt'
ls *copy*
Total: 5 < 10 & 3 > 2; #1
Total: 5 < 10 & 3 > 2; #1
weird name (copy) [1].txt

Common mistakes

Mistake Fix
if [ $name = "Ada" ] breaks when name is empty or has spaces if [ "$name" = "Ada" ]
cp $src $dest cp -- "$src" "$dest"
for f in $(ls *.txt) for f in *.txt
echo '$HOME' expecting the value echo "$HOME"

Test yourself

What does echo '$USER' print?

  1. Your user name
  2. $USER
  3. Nothing
  4. An error

f='my file.txt'. Which command removes that one file correctly?

  1. rm $f
  2. rm '$f'
  3. rm "$f"
  4. rm ${f}

What is the safest way to pass all of a script's arguments to another command?

  1. $*
  2. $@
  3. "$@"
  4. "$*"