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 %%.
@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:
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.
$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 printAda.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)
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