String Variables & Quoting Rules
In this page:
Single vs Double Quotes
Single quotes preserve text completely literally, so $name inside single quotes prints as the literal characters $name rather than being expanded. Double quotes still perform variable expansion, command substitution, and arithmetic expansion, while protecting the result from word splitting and globbing.
Example: Single vs Double Quotes
#!/bin/bash
name="Ada"
echo 'Hello, $name'
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Why Quoting Variables Matters
Leaving a variable unquoted lets Bash apply word splitting (breaking the value on spaces/tabs/newlines) and pathname expansion to it before the command sees it. Quoting the variable with double quotes keeps its value intact as a single argument, which is almost always what you want.
Warning: Unquoted $sentence collapses the multiple spaces into single spaces because word splitting discards extra whitespace; quoted "$sentence" preserves it exactly.
Example: Why Quoting Variables Matters
#!/bin/bash
sentence="multiple words here"
echo $sentence
echo "---"
echo "$sentence"
Login to try C/C++/Java/PHP code in the editor
Escaping a Single Quote
Because nothing can be escaped inside single quotes, embedding a literal single quote requires closing the quoted string, inserting the quote character another way, and reopening the quoted string. The common idiom is '...'\''...', which closes, escapes one quote, then reopens.
Example: Escaping a Single Quote
#!/bin/bash
message='It'\''s a sunny day'
echo "$message"
Login to try C/C++/Java/PHP code in the editor
Mixing Quotes and Expansions
It is common to combine literal text, variables, and command substitution inside a single double-quoted string, since double quotes allow all of Bash's expansions while still treating the whole thing as one argument. This keeps output readable without juggling many separate echo calls.
Example: Mixing Quotes and Expansions
#!/bin/bash
user="ada"
host=$(hostname)
echo "User $user is connected to $host at $(date +%H:%M:%S)"
Login to try C/C++/Java/PHP code in the editor
- Thinking single and double quotes are interchangeable; single quotes suppress ALL expansion (including
$variable), while double quotes still allow variable and command substitution. - Leaving a variable unquoted when it might contain spaces, e.g.
echo $nameinstead ofecho "$name", which causes word splitting and can break the command if the value has spaces or glob characters. - Trying to escape a single quote inside a single-quoted string with a backslash; that does not work in Bash -- you must close the quote, insert an escaped or double-quoted quote, then reopen it.
- Single quotes
'...'preserve every character literally -- no variable expansion, no command substitution. - Double quotes
"..."still expand variables ($var), command substitution ($(...)), and arithmetic ($(( ))), but suppress glob expansion and word splitting on the result. - Always double-quote variable references (
"$var") unless you specifically want word splitting/globbing to happen. - Unquoted strings are subject to word splitting and pathname expansion (globbing), which is rarely what you want for variables.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: