TExcelTAD
Home › Terminal & Command Line › Quoting & Escaping

Quoting & Escaping

Before a shell runs a command, it reads your line and decides what is a command name, what is an argument and which characters are special: spaces separate arguments, $ or % start variables, | and > set up pipes and redirection, * is a wildcard. Quoting and escaping are how you say "treat this character literally".

You need them every time a file name has a space (My Documents), a message contains & or !, or you pass a pattern that the shell must not expand.

CMD PowerShell bash zsh
Escape character ^ ` (backtick) \ \
Literal quotes none (only " exists) 'single' 'single' 'single'
Quotes that expand variables "%x%" (always expands) "double $x" "double $x" "double $x"
Quote marks passed to the program? yes, usually no no no

Windows (CMD)

CMD's quoting is the simplest and the oddest. Double quotes mainly group words with spaces and stop special characters like & and | from being interpreted, but the quotes themselves are kept. Everything else is escaped with a caret ^, and in batch files a literal % is written %%.

cmd · try it yourself
@echo off
mkdir "My Projects"
echo draft> "My Projects\plan.txt"
dir /b /o:n "My Projects"
echo Created a folder: "My Projects"
echo Fish ^& chips cost 5 ^> 4 coins
echo "Fish & chips"
echo 100%% sure
set "msg=Tom & Jerry"
echo "%msg%"
plan.txt
Created a folder: "My Projects"
Fish & chips cost 5 > 4 coins
"Fish & chips"
100% sure
"Tom & Jerry"

Notice that echo printed the quote marks: CMD hands them to the program as part of the text. Most programs (like dir and mkdir above) strip them from file names, but echo does not.

The quoted "%msg%" is important. %msg% is expanded before CMD parses the line, so without quotes the & inside the value splits the line into two commands:

cmd
C:\Users\ada> set "msg=Tom & Jerry"

C:\Users\ada> echo %msg%
Tom
'Jerry' is not recognized as an internal or external command,
operable program or batch file.

Delayed expansion (!msg!) is the other fix, because it expands after parsing. With delayed expansion turned on, ! itself becomes special and must be written ^^!. Details: Batch Escaping.

Windows (PowerShell)

PowerShell distinguishes two kinds of strings, and its escape character is the backtick, because \ is the Windows path separator.

powershell · try it yourself
$name = "Ada"
'Single quotes are literal: $name'
"Double quotes expand: $name"
"A subexpression: $($name.Length) letters, 2 + 3 = $(2 + 3)"
"Escapes: tab[`t] dollar[`$name] quote[`"]"
'It''s doubled inside single quotes'
$path = "C:\Program Files\App"
"Backslashes are ordinary: $path"
@"
A here-string keeps
  line breaks and "quotes", and expands $name.
"@
Single quotes are literal: $name
Double quotes expand: Ada
A subexpression: 3 letters, 2 + 3 = 5
Escapes: tab[	] dollar[$name] quote["]
It's doubled inside single quotes
Backslashes are ordinary: C:\Program Files\App
A here-string keeps
  line breaks and "quotes", and expands Ada.

A here-string follows the same rule as normal strings: @" ... "@ expands variables, @' ... '@ is literal.

  • $( ... ) inside double quotes runs any expression; "$name.Length" would print Ada.Length.
  • To call a native program with arguments that PowerShell would otherwise interpret, --% (stop-parsing) passes the rest of the line unchanged, e.g. icacls file --% /grant Users:(R).

Linux & macOS (bash/zsh)

bash · try it yourself
name="Ada Lovelace"
echo 'Single quotes are literal: $name *'
echo "Double quotes expand: $name"
echo "Escape inside double quotes: \$name \"quoted\" \\"
echo Backslash outside quotes: a\ b \& c
echo 
  

ANSI-C quotes: tab[\t] newline->\n<-'
echo "It's easy with double quotes"
echo 'It'\''s possible with single quotes too'
mkdir "my docs"
touch "my docs/a file.txt"
ls "my docs"
Single quotes are literal: $name *
Double quotes expand: Ada Lovelace
Escape inside double quotes: $name "quoted" \
Backslash outside quotes: a b & c
ANSI-C quotes: tab[	] newline->
<-
It's easy with double quotes
It's possible with single quotes too
a file.txt

Without the quotes, mkdir my docs would create two folders, my and docs.

Always quote your variables (bash)

The biggest bash quoting rule: an unquoted $var is split at spaces and its pieces are expanded as wildcards.

bash · try it yourself
file="annual report.txt"
touch "$file"
ls $file 2>&1
ls "$file"
ls: cannot access 'annual': No such file or directory
ls: cannot access 'report.txt': No such file or directory
annual report.txt

zsh is different

zsh does not split unquoted variables, so the same mistake works by accident. Scripts that must also run in bash should still quote. ${=var} asks zsh to split explicitly.

zsh · try it yourself
file="annual report.txt"
touch "$file"
ls $file
words="one two three"
for w in $words; do print "[$w]"; done
for w in ${=words}; do print "[$w]"; done
annual report.txt
[one two three]
[one]
[two]
[three]

Comparison

Goal CMD PowerShell bash / zsh
Path with spaces cd "My Projects" cd "My Projects" or 'My Projects' cd "My Projects" or My\ Projects
Literal text, no expansion ^%x^% (or %% in a .bat) '$x' '$x'
Expand a variable in text "%x%" "$x" "$x"
Escape one character ^& `$ \$
Literal quote inside quotes "" (in some programs) `" or "" \" in "..."
Newline in a string echo. on its own line "`n"
#39;\n'

Tip: When a command copied from the web fails, check the quotes first. Word processors and chat apps turn " and ' into curly quotes (“ ” ‘ ’), which no shell understands.

Learn more: Bash Quoting, Batch Escaping, PowerShell Strings.

Test yourself

Which PowerShell string prints $HOME literally, without expanding it?

  1. "$HOME"
  2. '$HOME'
  3. %HOME%
  4. $($HOME)

What is the escape character in CMD?

  1. \
  2. `
  3. ^
  4. %

In bash, file="my notes.txt"; cat $file fails. Why?

  1. cat can't read .txt files
  2. The unquoted variable is split into two arguments, my and notes.txt
  3. Variables can't contain dots
  4. The = needs spaces