Skip to main content

How to Keep Text Exactly as Typed with Single Quotes in bash and zsh

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

The shell changes some text before a command ever sees it: $name becomes the value of the variable name. Single quotes stop that, so the command gets the text exactly as you typed it. This guide prints the same greeting in double and in single quotes, with real output from zsh 5.9 and bash 3.2.

Syntax​

Syntax
'<text>'
PartWhat it means
'<text>'Passed on exactly as typed. $, spaces and every other character inside keep their literal value

The bash manual: "Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash."

Prerequisites​

  • zsh or bash. This guide uses zsh 5.9, the default shell on macOS, and checks every command in bash 3.2 too.

Step 1: Compare Double and Single Quotes​

Terminal
name="Ada"
echo "Hello, $name"
echo 'Hello, $name'
Output
Hello, Ada
Hello, $name

zsh setting name to Ada, then echo with double quotes printing Hello, Ada, and echo with single quotes printing Hello, $name.

In double quotes, the shell replaced $name with Ada before echo ran. In single quotes, it left the text alone, so echo printed $name itself. bash printed the same two lines.

Common Mistakes​

MistakeWhat happensDo this instead
An apostrophe inside single quotes: echo 'It's done'The apostrophe ends the quoted text, and the last ' opens a new one, so the shell waits for it to close: zsh showed a quote> prompt, and bash a > promptUse double quotes: echo "It's done" printed It's done

Troubleshooting​

SymptomLikely causeFix
A quote> prompt (zsh) or > prompt (bash) appearsA quote was opened and never closedPress Ctrl+C, which cancelled the line, and fix the quotes

FAQ​

When should I use double quotes instead?​

When you want the shell to replace variables, as "Hello, $name" did.

How do I keep an apostrophe inside single-quoted text?​

End the quoted text, add an escaped apostrophe, \', and start a new quoted part: echo 'It'\''s done' printed It's done.

Conclusion​

echo 'Hello, $name' printed $name exactly as typed, while echo "Hello, $name" printed Hello, Ada. Use single quotes when the shell must not change your text.

Additional Resources​