More Bash answers

by Danny
Published 2 min read

Single quotes keep text literal, double quotes still expand variables. Learn the difference and why unquoted variables break on spaces and wildcards.

The short answer

#

Single quotes keep everything inside them literal. Double quotes still expand variables, command substitutions and arithmetic, but stop the result being split on spaces or treated as a wildcard. Wrap variables in double quotes almost every time you use them.


_10
name="Ada Lovelace"
_10
_10
echo '$name' # prints: $name
_10
echo "$name" # prints: Ada Lovelace
_10
_10
touch "$name.txt" # one file called "Ada Lovelace.txt"
_10
touch $name.txt # two files: "Ada" and "Lovelace.txt"

Why it happens

#

Bash processes a command line in stages. After it expands a variable, it does two more things to the result if the expansion was not in double quotes:

  1. Word splitting. The value is split into separate words on the characters in IFS, which by default are space, tab and newline.
  2. Pathname expansion (globbing). Each word that contains *, ? or [ is treated as a pattern and replaced with matching file names.

So an unquoted variable is not one value. It is "whatever this turns into after splitting and globbing". That is rarely what you meant:


_10
file="My Report.pdf"
_10
rm $file # tries to remove "My" and "Report.pdf"
_10
_10
pattern="*"
_10
echo $pattern # lists every file in the current directory
_10
echo "$pattern" # prints: *

The three kinds of quoting behave like this:

QuotingVariables expand?Command substitution?Split and glob?
NoneYesYesYes
Double quotesYesYesNo
Single quotesNoNoNo

Inside double quotes, only $, backticks, \ and " keep a special meaning (and ! in an interactive shell with history expansion on). Inside single quotes, nothing does, not even a backslash.

How to fix it

#

Double quote every expansion

Treat this as the default for variables, command substitutions and arithmetic results:


_10
cp "$src" "$dest"
_10
files_count="$(ls | wc -l)"
_10
cd "$(dirname "$0")"

Nested quotes inside $(...) are fine. Each command substitution starts a fresh quoting context.

Use "$@" to pass arguments through

"$@" expands to every positional argument as a separate word, keeping spaces inside each one. $* and unquoted $@ both lose that.


_10
wrapper() {
_10
echo "Running with $# arguments"
_10
real_command "$@"
_10
}
_10
_10
wrapper "one arg" "two" # real_command gets exactly 2 arguments

The same applies to arrays: "${files[@]}" gives one word per element.

Use single quotes for literal text

Single quotes are ideal when the text contains characters you do not want Bash to touch, such as regular expressions, JSON, or another program's $ syntax:


_10
grep -E '^[0-9]+\.[0-9]+$' versions.txt
_10
jq '.dependencies | keys' package.json
_10
awk '{ print $2 }' data.txt

You cannot put a single quote inside single quotes, even escaped. Close the quote, add an escaped quote, and reopen it, or use double quotes:


_10
echo 'It'\''s working'
_10
echo "It's working"

Bash also supports ANSI-C quoting with $'...', which understands escapes such as \n and \t:


_10
printf '%s\n' $'line one\nline two'

Mix them when you need both

Adjacent quoted strings join into one word, so you can switch quoting styles mid-argument:


_10
echo 'Price: $5 for '"$name"

Let ShellCheck catch the rest

ShellCheck flags unquoted variables (warning SC2086) and many related mistakes. Running it in your editor or CI catches most quoting bugs before they reach a real file with a space in its name.

Common pitfalls

#
  • Assuming it works because your test file names had no spaces. Quote anyway; real paths will eventually contain spaces, brackets or asterisks.
  • Quoting a tilde: "~/projects" is not expanded. Use "$HOME/projects" or leave the tilde outside the quotes, as in ~/"my projects".
  • Quoting a glob you want to expand: ls "*.txt" looks for a file literally named *.txt. Leave the pattern unquoted and quote only the variable parts, as in ls "$dir"/*.txt.
  • Writing [ $var = "yes" ] with an empty variable. The test becomes [ = "yes" ] and errors. Quote it, or use [[ ... ]], which does not word-split its operands.
  • Expecting single quotes to expand $HOME or $(date) inside an echo or an alias. They will not.
#

Was this resource helpful?