15. Bash Quotes and Quotations
Overview¶
Bash uses different types of quotes to control how the shell interprets characters inside a command or string. Understanding quoting is important because characters such as $, ", ', `, \, *, and spaces can have special meanings in Bash.
The main quoting mechanisms are:
-
Escaping with
\ -
Single quotes
'...' -
Double quotes
"..." -
ANSI-C quoting
$'...'
Escaping Metacharacters¶
Bash has several characters with special meanings, known as metacharacters or special characters. A backslash \ can be used to escape a character and suppress its special meaning.
For example, $ normally performs variable expansion. Escaping it with \ makes Bash treat it as a literal $.
#!/bin/bash
BASH_VAR="Bash Script"
echo $BASH_VAR
# Escape $ so it is treated literally
echo \$BASH_VAR
# Escape the backslash itself
echo "\\"
Output:
How it works¶
Bash expands the variable:
While:
The \ prevents $ from being interpreted as the beginning of a variable expansion:
A backslash can also escape another backslash:
Output:
Common escaped characters¶
Escaping is especially useful when you need Bash to treat a special character literally.
Single Quotes¶
Single quotes preserve the literal value of almost everything inside them.
Output:
Inside single quotes, variable expansion does not happen.
For example:
prints:
instead of:
Single quotes treat special characters literally¶
The output contains the characters literally rather than expanding them.
Single quotes cannot directly contain another single quote¶
This does not work:
Bash interprets the second ' as the end of the quoted string.
One way to include a literal single quote is to close the single-quoted section, add an escaped quote, and reopen it:
Output:
Double Quotes¶
Double quotes preserve most characters literally, but they still allow certain Bash expansions.
The most important ones are:
-
Variable expansion with
$ -
Command substitution with
`...`or$(...) -
Certain backslash escapes
Example:
#!/bin/bash
BASH_VAR="Bash Script"
echo $BASH_VAR
echo "It's $BASH_VAR"
echo "The variable is \"$BASH_VAR\""
echo "Current date: `date`"
Example output:
Bash Script
It's Bash Script
The variable is "Bash Script"
Current date: Thu Feb 10 22:24:15 EST 2022
Variable expansion¶
Variables are expanded inside double quotes:
Output:
This is one of the most common uses of double quotes in Bash.
Double Quotes and Spaces¶
Double quotes are particularly important when variables contain spaces.
Consider:
Although this may appear to work with echo, unquoted expansion causes Bash to perform word splitting.
Instead, use:
This preserves the value as a single argument.
For example:
Without quotes:
Bash can interpret it as two arguments:
With quotes:
the filename remains:
General rule¶
When expanding variables, especially when their values may contain spaces, quote them:
Double Quotes and Literal Double Quotes¶
A double quote can be included inside a double-quoted string by escaping it with \.
Output:
Another example:
Output:
Single quotes do not need escaping inside double quotes:
Output:
Command Substitution¶
Double quotes still allow command substitution.
Modern Bash scripts generally use:
instead of backticks.
Example:
Or directly:
Example output:
The older backtick syntax also works:
However, $(...) is preferred because it is easier to read and nest.
Single Quotes vs Double Quotes¶
The difference can be demonstrated with a variable:
Output:
The reason is:
Another example:
Output:
Quick comparison¶
| Quoting | Variable expansion | Command substitution | Literal $ |
|---|---|---|---|
| No quotes | Yes | Yes | No |
'...' |
No | No | Yes |
"..." |
Yes | Yes | No |
$'...' |
ANSI-C escapes | No | Generally literal |
ANSI-C Quoting¶
Bash also supports ANSI-C quoting using:
This allows escape sequences such as \n, \t, \xNN, and \nnn to be interpreted.
Example:
Output:
The \n is interpreted as a newline.
Common ANSI-C Escape Sequences¶
| Escape | Meaning |
|---|---|
\a |
Alert/bell |
\b |
Backspace |
\e |
Escape character |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\\ |
Backslash |
\' |
Single quote |
\nnn |
Character represented by octal value |
\xnn |
Character represented by hexadecimal value |
ANSI-C Quoting Example¶
Output:
Here:
creates a tab, while:
creates a new line.
Using Hexadecimal Values¶
ANSI-C quoting can represent characters using hexadecimal values.
The hexadecimal value of @ is 40.
Output:
Using Octal Values¶
Characters can also be represented using octal values.
For example, the octal value 101 represents the character A.
Output:
Another example:
Output:
Practical Example¶
Quoting becomes particularly useful when working with filenames, variables, and special characters.
#!/bin/bash
USERNAME="Subrat Samantaray"
FILE="security report.txt"
echo "User: $USERNAME"
echo "File: $FILE"
echo 'Literal variable: $USERNAME'
echo $'Security Report\n==============\nUser: ' "$USERNAME"
Output:
User: Subrat Samantaray
File: security report.txt
Literal variable: $USERNAME
Security Report
==============
User: Subrat Samantaray
Common Quoting Mistakes¶
Forgetting quotes around variables¶
Avoid:
Prefer:
Quoting protects spaces and other characters in the variable value. The -- also prevents a filename beginning with - from being interpreted as an option.
Expecting variables to expand inside single quotes¶
Output:
Use double quotes when expansion is required:
Output:
Incorrectly nesting the same quote type¶
Avoid:
Use escaped double quotes:
Output:
Key Takeaways¶
\ → Escape a special character
'...' → Treat contents literally
"..." → Allow variable and command expansion while preserving most characters
$'...' → Enable ANSI-C escape sequences
The most important practical habit is to quote variable expansions when they represent a single value:
This prevents unexpected word splitting and helps make Bash scripts more predictable and robust.