Skip to content

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:

Bash Script
$BASH_VAR
\

How it works

echo $BASH_VAR

Bash expands the variable:

Bash Script

While:

echo \$BASH_VAR

The \ prevents $ from being interpreted as the beginning of a variable expansion:

$BASH_VAR

A backslash can also escape another backslash:

echo "\\"

Output:

\

Common escaped characters

echo "\$"
echo "\""
echo "\\"
echo "\*"
echo "\?"

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.

#!/bin/bash

BASH_VAR="Bash Script"

echo $BASH_VAR

echo '$BASH_VAR'
echo '$BASH_VAR "$BASH_VAR"'

Output:

Bash Script
$BASH_VAR
$BASH_VAR "$BASH_VAR"

Inside single quotes, variable expansion does not happen.

For example:

echo '$BASH_VAR'

prints:

$BASH_VAR

instead of:

Bash Script

Single quotes treat special characters literally

echo '$HOME'
echo '*.txt'
echo 'Hello $USER'
echo 'Hello `date`'

The output contains the characters literally rather than expanding them.

Single quotes cannot directly contain another single quote

This does not work:

echo 'It's Bash'

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:

echo 'It'\''s Bash'

Output:

It's Bash

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:

NAME="Subrat"

echo "Hello $NAME"

Output:

Hello Subrat

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:

NAME="Subrat Samantaray"

echo $NAME

Although this may appear to work with echo, unquoted expansion causes Bash to perform word splitting.

Instead, use:

echo "$NAME"

This preserves the value as a single argument.

For example:

FILE="my report.txt"

cat "$FILE"

Without quotes:

cat $FILE

Bash can interpret it as two arguments:

my
report.txt

With quotes:

cat "$FILE"

the filename remains:

my report.txt

General rule

When expanding variables, especially when their values may contain spaces, quote them:

"$VARIABLE"

Double Quotes and Literal Double Quotes

A double quote can be included inside a double-quoted string by escaping it with \.

echo "He said \"Hello\""

Output:

He said "Hello"

Another example:

NAME="Subrat"

echo "The name is \"$NAME\""

Output:

The name is "Subrat"

Single quotes do not need escaping inside double quotes:

echo "It's a Bash script"

Output:

It's a Bash script

Command Substitution

Double quotes still allow command substitution.

Modern Bash scripts generally use:

$(command)

instead of backticks.

Example:

DATE=$(date)

echo "Current date: $DATE"

Or directly:

echo "Current date: $(date)"

Example output:

Current date: Thu Feb 10 22:24:15 EST 2022

The older backtick syntax also works:

echo "Current date: `date`"

However, $(...) is preferred because it is easier to read and nest.


Single Quotes vs Double Quotes

The difference can be demonstrated with a variable:

NAME="Subrat"

echo '$NAME'
echo "$NAME"

Output:

$NAME
Subrat

The reason is:

'$NAME'    → literal text
"$NAME"    → variable expansion

Another example:

echo 'Today is $(date)'
echo "Today is $(date)"

Output:

Today is $(date)
Today is Thu Feb 10 22:24:15 EST 2022

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:

#!/bin/bash

echo $'Hello\nWorld'

Output:

Hello
World

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

#!/bin/bash

echo $'Name:\tSubrat\nRole:\tCybersecurity'

Output:

Name:   Subrat
Role:   Cybersecurity

Here:

\t

creates a tab, while:

\n

creates a new line.


Using Hexadecimal Values

ANSI-C quoting can represent characters using hexadecimal values.

The hexadecimal value of @ is 40.

echo $'Email: subrat\x40example.com'

Output:

Email: subrat@example.com

Using Octal Values

Characters can also be represented using octal values.

For example, the octal value 101 represents the character A.

echo $'\101'

Output:

A

Another example:

echo $'\110\145\154\154\157'

Output:

Hello

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:

rm $FILE

Prefer:

rm -- "$FILE"

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

NAME="Subrat"

echo 'Hello $NAME'

Output:

Hello $NAME

Use double quotes when expansion is required:

echo "Hello $NAME"

Output:

Hello Subrat

Incorrectly nesting the same quote type

Avoid:

echo "He said "Hello""

Use escaped double quotes:

echo "He said \"Hello\""

Output:

He said "Hello"

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:

"$VARIABLE"

This prevents unexpected word splitting and helps make Bash scripts more predictable and robust.