Linux Command Reference ยท Bash Syntax
Bash quoting
Reference for Bash quoting: backslash, single quotes, double quotes, $'...' ANSI-C strings and $"..." translation, with the rules for what each one expands.
Quoting tells Bash to treat characters literally instead of giving them a special meaning. Without quotes, spaces separate words, * expands to file names, $ starts a substitution and ; ends a command. With the right quotes, the same characters are just text.
Bash has four mechanisms: the backslash, single quotes, double quotes and the $'...' ANSI-C form. They differ in what they still allow to happen inside.
echo 'literal $HOME' # literal $HOME echo "expanded $HOME" # expanded /home/... echo $'tab:\there' # tab:<TAB>here
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
\c # backslash: next character literal 'text' # single quotes: everything literal "text" # double quotes: only $ ` \ ! (history) stay special $'text' # ANSI-C: backslash escapes are decoded
| Form | What still happens inside |
|---|---|
Backslash \c |
Removes the special meaning of the next character. A backslash before a newline continues the line. |
| Single quotes | Nothing. You cannot include a single quote, even with a backslash. |
| Double quotes | $var, ${...}, $(...), `...` and $((...)) expand. A backslash only escapes $, `, ", \ and newline. Globs and brace expansion do not happen. |
$'...' |
Escapes such as \n, \t, \\, \', \", \xHH, \uHHHH, \nnn and \cx are replaced; no variable expansion. |
Returns: the quoted text as one or more words. Edge cases: the exclamation mark triggers history expansion in interactive double-quoted strings ("hello!" may fail); single quotes or set +H avoid it.
Usage notes
- Quote every expansion unless you want splitting:
"$var","$(cmd)","${arr[@]}","$@". This single habit prevents most shell bugs, especially with file names containing spaces. - Double quotes keep newlines.
echo "$(cat file)"preserves line breaks; unquoted, the lines collapse into one because of word splitting. - Tilde and brace expansion do not occur inside quotes.
"~"is a literal tilde;"$HOME"is the right way. Likewise"{a,b}"stays literal. - Patterns on the right of
[[ == ]]: quoting makes the pattern literal, leaving it unquoted makes it a glob. See[[ ]]conditions. - A glob inside quotes does not expand:
ls "*.txt"looks for a file literally named*.txt. - Embedding a single quote is easiest with
$'...'or the close-escape-reopen idiom'\''. - Never build commands from quoted user input and pass them to
eval. Use arrays, orprintf %qand${var@Q}, which produce reusable quoted forms. Seeeval. - Nested quotes inside
$( )start a fresh context, so"$(echo "inner")"works without escaping.
Related
- Parameter expansion, Word splitting, Globbing,
printf,echo - Lessons: Quoting, Expansion order