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

In Bash, quoting controls whether characters are treated as syntax or literal data. Use single quotes for completely literal text, double quotes to allow selected expansions while preventing word splitting and pathname expansion, a backslash to protect one character, and ANSI-C quoting ($'...') when you need escapes such as a newline. Correct quoting keeps file names, variable values, and command arguments intact.

The rules below follow the GNU Bash Reference Manual for Bash 5.3 (updated 18 May 2025). Some forms, especially ANSI-C quoting, are Bash features and should not be assumed to work in a strictly POSIX sh script.

What Bash quoting does

Before Bash runs a command, it recognizes shell syntax, performs expansions, splits unquoted results into words, expands unquoted wildcard patterns, and removes the quotes themselves. Quoting changes which characters participate in those stages. It does not add quote characters to the argument received by the command.

For example, these commands pass different arguments:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
printf '<%s>n' one two
printf '<%s>n' "one two"
printf '<%s>n' 'one two'

The first command supplies two arguments; the other two supply one argument containing a space.

The four Bash quoting mechanisms

Backslash: protect the next character

An unquoted backslash removes the special meaning of the character immediately after it. This is useful for one space, dollar sign, wildcard, quote, or other metacharacter:

printf '%sn' file name
printf '%sn' $HOME
printf '%sn' *.log

A backslash followed by a newline is removed entirely. Bash therefore treats the next physical line as a continuation of the current command:

printf '%sn' first 
  second

Inside double quotes, backslash retains special meaning only before $, backtick, double quote, backslash, or newline. Before other characters it remains literal. Inside single quotes, a backslash has no special meaning at all.

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

Single quotes: make every character literal

Between single-quote characters, every character retains its literal value. Variables, command substitutions, backslashes, wildcards, and whitespace are not interpreted:

name='Ada $USER'
printf '%sn' '$HOME' '*.txt' '$(date)'

Those examples print the characters $HOME, *.txt, and $(date), rather than expanding them.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

A single quote cannot appear inside a single-quoted span, even when preceded by a backslash. End the span, add an escaped or differently quoted quote, and then reopen it:

message='It'7s Bash'
printf '%sn' "$message"

Here the adjacent pieces produce It's Bash. Another readable option is to use double quotes when the text contains a single quote but no expansion that you want to suppress:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
message="It's Bash"

For arbitrary text that may contain both quote types, store pieces in variables or use a quoted here-document rather than trying to place an unescaped delimiter inside single quotes.

Double quotes: protect a word but permit selected expansions

Double quotes preserve the literal value of most characters and keep the result as one shell word. They still allow:

  • parameter expansion, such as $name or ${name};
  • command substitution, such as $(date) or legacy backticks;
  • arithmetic expansion, such as $((count + 1));
  • backslash escapes for $, backtick, double quote, backslash, and newline.
name='Ada Lovelace'
printf 'Hello, %sn' "$name"
printf 'Today is %sn' "$(date +%F)"

Double-quote expansions whenever you want one argument. Without quotes, a value containing spaces, tabs, newlines, or wildcard characters can become multiple arguments or expand to matching pathnames.

file='notes from Monday.txt'
cat -- "$file"     # one filename argument
cat -- $file        # usually several words; may also glob

Use "$@" to forward positional parameters one by one, preserving each argument. "$*" instead joins the positional parameters into one word using the first character of IFS (a space by default).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

ANSI-C quoting: $'...'

Bash’s ANSI-C quoting expands backslash escapes inside the quoted text, then treats the resulting value as one word. Common escapes include n for a newline, t for a tab, \ for a backslash, ' for a single quote, and " for a double quote.

printf '%s' $'first linensecond linen'
quote=$'It's quoted'

The exact escape repertoire is Bash-specific; do not use this form when the script must run under an implementation that only promises POSIX sh.

Single quotes versus double quotes

Form Parameter, command, and arithmetic expansion Word splitting and pathname expansion Typical use
'text' No No Completely literal text
"text" Yes No for the quoted result One argument containing a variable or substitution
x Protects only the next character Depends on the rest of the word Escape one metacharacter or space
$'text' Backslash escape processing No for the resulting quoted word Newlines, tabs, and special characters in Bash scripts

How quoting stops variable expansion

To print a dollar sign literally, either single-quote it or escape it:

printf '%sn' '$HOME'
printf '%sn' $HOME

To expand a variable safely, use double quotes around the expansion:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf 'User: %sn' "$USER"
printf 'Path: %sn' "${HOME}/bin"

Braces delimit the variable name when adjacent characters could otherwise be interpreted as part of it. They do not replace quoting:

prefix='report'
printf '%sn' "${prefix}_2026.txt"

Command and arithmetic substitutions follow the same rule: quote the complete substitution when its output must remain one argument.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
result="$(some_command)"
printf '%sn' "$result"
count=$((width * height))

Why a quoted variable can still behave unexpectedly

Only the expansion is quoted

Quoting one expansion does not quote other text in the same word. Keep the entire argument in one pair of double quotes:

dir='/tmp/my files'
rm -- "$dir"/*.tmp

In this example, the directory value is protected, while the final *.tmp is intentionally left unquoted so Bash can perform pathname expansion. Quote the pattern too when you want a literal asterisk:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf '%sn' "$dir/*.tmp"

Arrays require the right expansion

For an array, "${items[@]}" expands to one word per element, preserving empty elements and embedded whitespace. "${items[*]}" expands to one word containing all elements joined by the first IFS character.

items=('red blue' green '')
printf '<%s>n' "${items[@]}"
printf '<%s>n' "${items[*]}"

Quoting cannot undo an earlier transformation

If data was already split, glob-expanded, or interpreted as shell code before it reached a variable, adding quotes later cannot reconstruct the original text. Do not build a command string and then execute it with eval unless you have a compelling, carefully audited reason. Prefer arrays for commands and arguments:

cmd=(grep --fixed-strings -- 'a b' 'notes/*.txt')
"${cmd[@]}"

Quoting common special characters

  • Whitespace: use "a b", 'a b', or escape the space as a b.
  • Wildcard characters: quote *, ?, and bracket expressions when they must remain literal.
  • Semicolon, pipe, ampersand, redirection, and parentheses: quote or escape them when they are data rather than shell operators.
  • Hash: an unquoted # can begin a comment where Bash recognizes a comment; quote it when it is part of data.
  • Newline: use $'n', a quoted here-document, or a variable containing a newline rather than an accidental command terminator.
printf '%sn' 'a; b | c & d > file'
printf '%sn' 'literal *.log'
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Quotes in here-documents

A here-document delimiter controls whether its body is expanded. With an unquoted delimiter, parameter expansion, command substitution, arithmetic expansion, and backslash processing can occur in the body:

cat <<EOF
User: $USER
EOF

Quote any part of the delimiter to suppress those expansions and pass the body literally:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
cat <<'EOF'
User: $USER
$(date)
EOF

The closing delimiter must appear by itself at the start of a line (subject to the here-document indentation options you explicitly use). A quoted delimiter is especially useful for configuration files, scripts, and examples that contain dollar signs.

Practical quoting patterns

Pass a filename safely

filename=$1
if [[ -e $filename ]]; then
  printf 'Found: %sn' "$filename"
fi

Within Bash’s [[ ... ]] conditional syntax, ordinary word splitting and pathname expansion are not performed in the same way as in ordinary command arguments, but quoting variable expansions remains a clear and portable habit, especially when the value is reused outside [[ ]].

Read lines without losing whitespace

while IFS= read -r line; do
  printf '<%s>n' "$line"
done < input.txt

read -r prevents backslash interpretation, and quoting "$line" preserves the line’s whitespace when it is printed.

Use a literal regular-expression or search pattern

pattern='[draft] $HOME'
grep --fixed-strings -- "$pattern" notes.txt

Single quotes make the assignment literal; double quotes preserve the resulting pattern as one argument.

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

Quoting checklist

  • Do you want literal text with no expansion? Use single quotes.
  • Do you need variable or command expansion but one argument? Use double quotes.
  • Do you need a newline, tab, or escaped quote in Bash? Consider $'...'.
  • Are you escaping only one metacharacter? A backslash may be simplest.
  • Could a value contain spaces, newlines, wildcard characters, or be empty? Quote its expansion.
  • Are you forwarding script arguments? Use "$@", not unquoted $@.
  • Are you constructing a command? Use an array instead of a string and avoid eval.
  • Should a here-document be literal? Quote its delimiter.

Frequently Asked Questions

How do I include a single quote inside a Bash single-quoted string?

End the single-quoted span, add the quote using another quoted or escaped segment, and reopen it: 'It'''s' produces It's. For simple text, double quotes or $'...' may be clearer.

What is the safest way to expand a Bash variable?

Use double quotes around the expansion, such as "$value" or "${dir}/file", unless you deliberately need word splitting or pathname expansion.

Why does echo "$var" still show a wildcard or dollar sign literally?

Double quotes allow parameter, command, and arithmetic expansion, but they do not recursively interpret the characters produced by a variable as new shell syntax. A value containing * or $ remains data.

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.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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