Sagar.BlogArticle
All posts
All posts
Bash

Quoting in Bash — Single, Double, and Escape

Quoting controls how Bash interprets special characters. Knowing when to use single quotes, double quotes, or backslash escapes prevents the most common scripting bugs.

January 5, 20266 min read
BashQuotingScripting

Quoting in Bash is one of the biggest sources of subtle bugs. The three quoting mechanisms — single quotes, double quotes, and backslash — each behave differently. Once you understand why, you'll write airtight scripts.

Double Quotes — Expand Variables

Double quotes preserve most special characters except variable expansion ($) and command substitution ($()). Use them to protect strings that contain spaces while still allowing variable interpolation.

name="Alice"
echo "Hello, $name!"         # Hello, Alice!    — variable expanded
echo "Today: $(date +%F)"    # Today: 2026-01-05  — command substituted
echo "Price: $5"            # Price: $5   — backslash escapes the $
echo "Tab:	here"            # Tab:  here  — escape sequences work

Single Quotes — Literal String

Single quotes are the strongest form of quoting. Nothing is interpreted inside single quotes — not $, not \, not $(). What you see is literally what you get.

name="Alice"
echo 'Hello, $name!'         # Hello, $name!  — no expansion
echo 'Price: $5'             # Price: $5
echo 'Path: /home/$USER'     # Path: /home/$USER  — literal dollar

# You CANNOT include a single quote inside single quotes
# Workaround: end single-quote, add escaped quote, start again
echo 'don'"'"'t'             # don't

When to use each

Double quotes → when you need variable or command substitution inside the string. Single quotes → when the string should be completely literal (regex patterns, awk scripts, SQL, special characters).

Backslash — Escape a Single Character

echo "Cost: $100"         # Cost: $100  — $ is literal
echo "Path: $HOME"        # Path: $HOME — $ is literal

# In unquoted context
echo Hello World          # Hello World — space is literal
ls my file.txt            # handles filename with space

Word Splitting — Why Quoting Matters

Without quotes, Bash performs word splitting — it breaks unquoted variable expansions on spaces, tabs, and newlines. This causes bugs with filenames or strings containing spaces.

files="file1.txt file2.txt"

# WRONG — Bash sees TWO arguments
for f in $files; do echo "$f"; done
# file1.txt
# file2.txt
# OK here, but dangerous in general

path="/home/user/my documents"
ls $path     # ❌ tries: ls /home/user/my  documents  (TWO args)
ls "$path"   # ✅ tries: ls "/home/user/my documents" (ONE arg)

The $'...' Quoting Form

There's a third quoting style: $'...' (ANSI-C quoting). It's like single quotes but processes escape sequences like \n, \t, \033 (ANSI color codes).

echo $'Line 1
Line 2'   # prints two lines
echo $'col1	col2'       # tab-separated
echo $'\033[32mGreen\033[0m'  # ANSI green text
Quick Check

What does `echo '$USER is: $USER'` print?

Exercise

Experiment with quoting. For each line, predict what it prints, then run it:

  1. name="World"; echo "Hello $name"
  2. name="World"; echo 'Hello $name'
  3. echo "Price is \$5"
  4. dir="my folder"; ls $dir (create a folder named "my folder" first with mkdir "my folder")