Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

In Bash, quoting controls which characters the shell treats as syntax and which it passes as text. Use single quotes to preserve every character literally, double quotes to prevent word splitting and glob expansion while still allowing variable and command substitutions, and backslashes to protect individual characters. Bash also supports ANSI-C quoting with $'…'.

What quoting changes in Bash

Bash processes command text before starting a command. Characters such as spaces, dollar signs, asterisks, and quotes can affect parsing, expansion, or how arguments are formed. Quoting protects characters from some or all of those operations. It does not generally add quote characters to the argument: the quotes are shell syntax and are removed during processing.

The examples here describe GNU Bash 5.3, as documented in the GNU Bash Reference Manual, last updated May 18, 2025. Bash-specific forms such as $'…' should not be assumed to work in every shell or in POSIX sh.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Single quotes: preserve text literally

Within single quotes, every enclosed character keeps its literal value. Bash does not expand variables or command substitutions, interpret backslashes as escapes, or treat spaces and wildcard characters as shell syntax.

message='Price: $5; path is *.txt'

In this assignment, message receives the literal text Price: $5; path is *.txt. The dollar sign and asterisk do not trigger expansion.

Including a single quote

A single-quoted span cannot contain a single quote—not even by putting a backslash before it. Instead, end the span, add an escaped quote outside it, and start another span:

text='It's Bash'

Here the backslash protects the apostrophe outside the single-quoted spans. Bash joins the adjacent quoted and unquoted pieces into one word, producing It's Bash.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Double quotes: protect words, allow selected expansions

Double quotes keep spaces and wildcard characters from splitting an argument or expanding as filename patterns, but they do not make everything literal. Bash still performs parameter expansion, command substitution, and arithmetic expansion inside double quotes. Backticks also retain their command-substitution meaning.

name='Ada Lovelace'
printf '%sn' "$name"

The quoted expansion is passed as one argument, including the space. By contrast, "$name" still expands the variable; use single quotes or escape the dollar sign when you need the literal characters $name.

Inside double quotes, a backslash only protects $, backtick, double quote, backslash, or newline. Before other characters, the backslash remains part of the text.

Backslashes: escape a character or continue a line

An unquoted backslash protects the character immediately after it, removing that character’s special meaning. For instance, file name.txt forms one word containing a space. An unquoted backslash followed by a newline is removed, allowing a command to continue onto the next physical line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf '%sn' one 
  two

The line continuation joins the lines; it does not add a newline argument between one and two. A backslash-newline inside single quotes is literal text. Within double quotes, backslash-newline is also removed.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

ANSI-C quoting: Bash’s $'…' form

Bash’s dollar-single-quote form interprets backslash escapes in the enclosed text. It is useful when a string needs characters such as a newline or tab, or an escaped quote, without writing a literal line break.

printf '%sn' $'first linensecond line'

The escape n becomes a newline in the resulting string. This is Bash-specific syntax; check the target shell before using it in a script intended for other shells.

Why a quoted argument may still behave unexpectedly

Quoting protects text during specific shell-processing stages; it does not make a command’s own interpretation disappear. A command may interpret an argument as an option, pattern, expression, or filename after Bash has passed it. Likewise, double-quoting a variable prevents word splitting and pathname expansion on that expansion, but does not stop the variable from expanding.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Use "$var" when the variable’s value should be one argument, even if it contains spaces or wildcard characters.
  • Use '$var' when you want the literal characters $var, not the variable’s value.
  • Use $ outside quotes or "$" inside double quotes to protect a dollar sign in a short literal.
  • Use single quotes for literal text, but end the single-quoted span to include an apostrophe.

Quick comparison

Form Variable and command substitutions Spaces and wildcard characters Typical use
'text' Not performed Kept literal Text that must not expand
"text" Performed Do not split or expand as filename patterns One argument containing expanded values
x outside quotes Protects the next character Depends on which character is protected Escaping an individual special character
$'text' Backslash escapes are interpreted Quoted as one word Bash-specific escaped characters such as newlines

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.