The Three Types of Quotes
The Three Types of Quotes
Proper quoting is the single most critical defensive skill in Bash. Bash supports three quoting mechanisms, each with distinct semantic rules:
1. Single Quotes ('...')
Single quotes preserve the literal value of every character within the quotes. No expansions occur—no parameter substitution, no command substitution, no arithmetic expansion, and no escape sequences.
# Literal string preservation
name="Alice"
echo 'Hello, $name! Today is $(date).'
# Output: Hello, $name! Today is $(date).Note: You cannot include a single quote inside single quotes, even with a backslash (' does not work). To include a literal single quote, close the quote, insert an escaped quote, and reopen: 'Don'''t touch this'.
2. Double Quotes ("...")
Double quotes suppress word splitting and globbing, but allow: * Parameter expansion ($VAR, ${VAR}) * Command substitution ($(command)) * Arithmetic expansion ($(( ... ))) * Escaped dollar signs (\$), backticks (\``), double quotes ("), and backslashes (\`).
greeting="Hello"
user="Bob"
echo "$greeting, $user! PID is $$"
# Output: Hello, Bob! PID is 12343. ANSI-C Quoting ($'...')
ANSI-C quoting expands C-style backslash escape sequences into literal binary characters before execution:
# Newlines, tabs, and hex characters
newline_string=$'Line 1\nLine 2\tIndented'
echo "$newline_string"
# Carriage return in terminal status lines
printf $'Downloading...\r'