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.
_10name="Ada Lovelace"_10_10echo '$name' # prints: $name_10echo "$name" # prints: Ada Lovelace_10_10touch "$name.txt" # one file called "Ada Lovelace.txt"_10touch $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:
- Word splitting. The value is split into separate words on the characters in
IFS, which by default are space, tab and newline. - 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:
_10file="My Report.pdf"_10rm $file # tries to remove "My" and "Report.pdf"_10_10pattern="*"_10echo $pattern # lists every file in the current directory_10echo "$pattern" # prints: *
The three kinds of quoting behave like this:
| Quoting | Variables expand? | Command substitution? | Split and glob? |
|---|---|---|---|
| None | Yes | Yes | Yes |
| Double quotes | Yes | Yes | No |
| Single quotes | No | No | No |
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:
_10cp "$src" "$dest"_10files_count="$(ls | wc -l)"_10cd "$(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.
_10wrapper() {_10 echo "Running with $# arguments"_10 real_command "$@"_10}_10_10wrapper "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:
_10grep -E '^[0-9]+\.[0-9]+$' versions.txt_10jq '.dependencies | keys' package.json_10awk '{ 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:
_10echo 'It'\''s working'_10echo "It's working"
Bash also supports ANSI-C quoting with $'...', which understands escapes such as \n and \t:
_10printf '%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:
_10echo '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 inls "$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
$HOMEor$(date)inside anechoor an alias. They will not.
Related reading
#Was this resource helpful?