← Back to Bash Course | Chapter 2: Variables & Types | Lesson 2 of 13

String Variables & Quoting Rules

Quotes control whether Bash treats text literally or looks inside it for variables and other special expansions.

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

bash
#!/bin/bash
name="Ada"
echo 'Hello, $name'
echo "Hello, $name"

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

bash
#!/bin/bash
sentence="multiple   words   here"
echo $sentence
echo "---"
echo "$sentence"

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

bash
#!/bin/bash
message='It'\''s a sunny day'
echo "$message"

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

bash
#!/bin/bash
user="ada"
host=$(hostname)
echo "User $user is connected to $host at $(date +%H:%M:%S)"
Common Mistakes
  1. Thinking single and double quotes are interchangeable; single quotes suppress ALL expansion (including $variable), while double quotes still allow variable and command substitution.
  2. Leaving a variable unquoted when it might contain spaces, e.g. echo $name instead of echo "$name", which causes word splitting and can break the command if the value has spaces or glob characters.
  3. 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.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.