Quoting in Bash
last modified October 24, 2026
Quoting controls how the shell treats special characters such as spaces,
$, *, and |. Unquoted, they split
words, expand variables, match files, and connect commands. Most shell
bugs, from scripts that break on file names with spaces to commands that
delete the wrong files, come from missing or wrong quotes. Bash has
single quotes, double quotes, the backslash, and the ANSI-C form
$'...'.
The examples were run with Bash 5.2 on Debian 12. printf
'[%s]\n' is used in several places to show the separate arguments
that a command receives.
Why quoting matters
The shell splits unquoted text into words at spaces. A variable holding a file name with a space becomes two arguments.
f="my file.txt" touch $f ls rm my file.txt touch "$f" ls -l "$f"
touch $f created two files, my and
file.txt. With double quotes, "$f" is a single
argument. Quoting variables is the most important rule of shell
scripting.
$ f="my file.txt" $ touch $f $ ls app.log args.sh file.txt my people.txt $ rm my file.txt $ touch "$f" $ ls -l "$f" -rw-r--r-- 1 jano jano 0 Oct 24 22:04 my file.txt
Single quotes
Everything between single quotes is literal. No character is special, not even the backslash.
echo 'Price: $5, command: $(date), newline: \n' echo 'It'\''s fine' echo "It's fine"
Because nothing is special inside, a single quote cannot appear in a
single-quoted string. The usual workaround ends the string, adds an
escaped quote \', and starts a new string; the parts are
joined into one word. Double quotes are often simpler for such text.
$ echo 'Price: $5, command: $(date), newline: \n' Price: $5, command: $(date), newline: \n $ echo 'It'\''s fine' It's fine $ echo "It's fine" It's fine
Double quotes
Inside double quotes, the shell still expands variables
($), command substitutions ($( ) and
backticks), and arithmetic, but no word splitting or globbing happens.
name=Jan echo "Hello $name, today is $(date +%A)" echo "Cost: \$5, quote: \", backslash: \\" echo "Tab: \t stays" echo "Home: ~ stays, $HOME expands"
A backslash inside double quotes escapes only $,
`, ", \, and a newline; before
other characters, such as t, it stays as it is. The tilde is
not expanded inside quotes; use $HOME. Double quotes are the
right default for any expansion.
$ name=Jan $ echo "Hello $name, today is $(date +%A)" Hello Jan, today is Saturday $ echo "Cost: \$5, quote: \", backslash: \\" Cost: $5, quote: ", backslash: \ $ echo "Tab: \t stays" Tab: \t stays $ echo "Home: ~ stays, $HOME expands" Home: ~ stays, /home/jano expands
ANSI-C quoting
Strings of the form $'...' interpret backslash escapes like
C and printf.
echo $'Tab:\there\nsecond line' echo $'It\'s fine' echo $'caf\u00e9' printf '%s\n' $'\e[1mbold\e[0m' | cat -v
\t and \n become a tab and a newline,
\' a single quote, \u00e9 a Unicode character,
and \e the escape character used in terminal colour codes,
made visible here by cat -v as ^[. This form is
convenient for strings with control characters, such as
IFS=$'\n'.
$ echo $'Tab:\there\nsecond line' Tab: here second line $ echo $'It\'s fine' It's fine $ echo $'caf\u00e9' café $ printf '%s\n' $'\e[1mbold\e[0m' | cat -v ^[[1mbold^[[0m
The backslash
Outside quotes, a backslash makes the next character literal. At the end of a line, it continues the command on the next line.
echo a\ \ b echo \$HOME \* \" echo one \ two \ three
Escaped spaces are kept, so a\ \ b is one argument with
two spaces. For longer strings, quotes are easier to read than many
backslashes. The line continuation must be the very last character on
the line; a space after it breaks the command.
$ echo a\ \ b a b $ echo \$HOME \* \" $HOME * " $ echo one \ > two \ > three one two three
Splitting and empty values
Unquoted expansions are split into words, and empty ones disappear
entirely. The function nargs counts its arguments.
var="a b"
echo $var
echo "$var"
nargs() { echo "$# arguments"; }
empty=""
nargs $empty
nargs "$empty"
nargs $var
nargs "$var"
echo $var receives two words and joins them with one space,
losing the original spacing. An unquoted empty variable produces no
argument at all, which breaks commands that expect one, such as
[ $x = yes ]; quoted, it is an empty argument. See the
Bash conditionals article.
$ var="a b"
$ echo $var
a b
$ echo "$var"
a b
$ nargs() { echo "$# arguments"; }
$ empty=""
$ nargs $empty
0 arguments
$ nargs "$empty"
1 arguments
$ nargs $var
2 arguments
$ nargs "$var"
1 arguments
"$@" and "$*"
The script arguments are available as $@ and
$*. Quoted and unquoted, they give four different
results.
#!/bin/bash
show() {
echo " $# args:" "$(printf '[%s] ' "$@")"
}
echo '"$@":'; show "$@"
echo '"$*":'; show "$*"
echo '$@:'; show $@
echo '$*:'; show $*
"$@" keeps each argument exactly as given, including
two words; it is almost always what you want, for example
to pass arguments on to another command. "$*" joins all
arguments into one string, separated by the first character of
IFS. Unquoted, both forms split everything into words.
$ bash args.sh one "two words" three "$@": 3 args: [one] [two words] [three] "$*": 1 args: [one two words three] $@: 4 args: [one] [two] [words] [three] $*: 4 args: [one] [two] [words] [three]
Arrays
Arrays follow the same rules: "${array[@]}" gives each
element as one word.
files=("annual report.pdf" "notes.txt")
touch "${files[@]}"
for f in ${files[@]}; do echo "[$f]"; done
for f in "${files[@]}"; do echo "[$f]"; done
echo "${files[*]}"
(IFS=,; echo "${files[*]}")
Without quotes, annual report.pdf is split. "${files[*]}"
joins the elements into one string with the first character of
IFS, which makes it a simple way to join a list with
commas; the subshell keeps the changed IFS local.
$ files=("annual report.pdf" "notes.txt")
$ touch "${files[@]}"
$ for f in ${files[@]}; do echo "[$f]"; done
[annual]
[report.pdf]
[notes.txt]
$ for f in "${files[@]}"; do echo "[$f]"; done
[annual report.pdf]
[notes.txt]
$ echo "${files[*]}"
annual report.pdf notes.txt
$ (IFS=,; echo "${files[*]}")
annual report.pdf,notes.txt
Quotes inside command substitution
A command substitution starts a new quoting context. Quotes inside
$( ) do not end the quotes outside.
dir="my photos" mkdir "$dir"; touch "$dir/a.jpg" "$dir/b.jpg" echo "Files in $dir: $(ls "$dir" | wc -l)" echo "Parent: $(basename "$(dirname "$PWD/$dir")")"
Each level is quoted on its own, so nested substitutions remain correct
for names with spaces. This is one more reason to prefer
$( ) over backticks.
$ dir="my photos" $ mkdir "$dir"; touch "$dir/a.jpg" "$dir/b.jpg" $ echo "Files in $dir: $(ls "$dir" | wc -l)" Files in my photos: 2 $ echo "Parent: $(basename "$(dirname "$PWD/$dir")")" Parent: work
Quotes stored in variables
Quotes inside a variable's value are ordinary characters; the shell does not interpret them again when the variable is expanded.
opts='-l "annual report.pdf"'
ls $opts
ls "$opts"
opts=(-l "annual report.pdf")
ls "${opts[@]}"
Unquoted, the value was split at the space, and the quote characters became part of the file names. Quoted, it was a single strange option. To store a list of arguments, use an array.
$ opts='-l "annual report.pdf"'
$ ls $opts
ls: cannot access '"annual': No such file or directory
ls: cannot access 'report.pdf"': No such file or directory
$ ls "$opts"
ls: invalid option -- ' '
Try 'ls --help' for more information.
$ opts=(-l "annual report.pdf")
$ ls "${opts[@]}"
-rw-r--r-- 1 jano jano 0 Oct 24 22:04 annual report.pdf
Quoting arguments for other programs
Patterns and programs for grep, awk,
sed, and find contain characters that the shell
would interpret. Put them in single quotes.
grep -c error|warn app.log
grep -c -E 'error|warn' app.log
awk "{ print $1 }" people.txt
awk '{ print $1 }' people.txt
Unquoted, | is a pipe, so the shell tried to run a command
named warn. The awk commands were typed in an
interactive shell. In double quotes, the shell replaced $1
with its own first positional parameter, which is empty, so
awk received the program { print } and printed
whole lines. Single quotes pass the program to awk
unchanged, and $1 is the first field.
$ grep -c error|warn app.log
bash: warn: command not found
$ grep -c -E 'error|warn' app.log
3
$ awk "{ print $1 }" people.txt
Peter 34
Lucia 28
$ awk '{ print $1 }' people.txt
Peter
Lucia
Quotes and patterns
In [[ ]] and case, quoting decides whether a
string is a pattern or literal text.
f="notes.txt" [[ $f == *.txt ]] && echo "pattern match" [[ $f == "*.txt" ]] || echo "literal: no match" pat="*.txt" [[ $f == $pat ]] && echo "unquoted variable: pattern" [[ $f == "$pat" ]] || echo "quoted variable: literal"
On the right side of ==, an unquoted string or variable is a
glob pattern; quoted, it is compared literally. This is the one place
where leaving a variable unquoted is intended.
$ f="notes.txt" $ [[ $f == *.txt ]] && echo "pattern match" pattern match $ [[ $f == "*.txt" ]] || echo "literal: no match" literal: no match $ pat="*.txt" $ [[ $f == $pat ]] && echo "unquoted variable: pattern" unquoted variable: pattern $ [[ $f == "$pat" ]] || echo "quoted variable: literal" quoted variable: literal
Here documents
Quoting the delimiter of a here document turns off all expansions in its body.
cat <<EOF Home: $HOME Sum: $((2 + 2)) EOF cat <<'EOF' Home: $HOME Sum: $((2 + 2)) EOF
With <<EOF, the body behaves like a double-quoted
string. With <<'EOF', it is taken literally, which is
what you want when generating scripts or configuration files that
contain $. See the redirection
article.
$ cat <<EOF > Home: $HOME > Sum: $((2 + 2)) > EOF Home: /home/jano Sum: 4 $ cat <<'EOF' > Home: $HOME > Sum: $((2 + 2)) > EOF Home: $HOME Sum: $((2 + 2))
Two shells: ssh
A command given to ssh is parsed twice: by the local shell,
and then, as a single string, by the shell on the remote machine.
greeting="hello from the client" ssh box "echo $greeting" ssh box 'echo "[$greeting]"' file="annual report.pdf" ssh box "ls -l ~/work/$file" ssh box "ls -l ~/work/$(printf '%q' "$file")"
In double quotes, the local shell expanded $greeting
before sending the command. In single quotes, the remote shell expanded
it, and the variable does not exist there. The file name with a space
survived the local quotes, but the remote shell split it again.
printf '%q' quotes a value so that a shell reads it back
unchanged, which makes it safe to embed in a remote command.
$ greeting="hello from the client" $ ssh box "echo $greeting" hello from the client $ ssh box 'echo "[$greeting]"' [] $ file="annual report.pdf" $ ssh box "ls -l ~/work/$file" ls: cannot access '/home/jano/work/annual': No such file or directory ls: cannot access 'report.pdf': No such file or directory $ ssh box "ls -l ~/work/$(printf '%q' "$file")" -rw-r--r-- 1 jano jano 0 Oct 24 22:04 /home/jano/work/annual report.pdf
Two shells: bash -c
The same happens with bash -c, sh -c,
su -c, and find -exec sh -c.
file="annual report.pdf" bash -c "ls -l $file" bash -c 'ls -l "$1"' _ "$file"
Instead of pasting the value into the command string, pass it as an
argument: after the script, the first word (_) becomes
$0 and the following ones $1, $2,
and so on. The script itself is in single quotes, and the value never
gets parsed as code, which also prevents injection through crafted file
names.
$ file="annual report.pdf" $ bash -c "ls -l $file" ls: cannot access 'annual': No such file or directory ls: cannot access 'report.pdf': No such file or directory $ bash -c 'ls -l "$1"' _ "$file" -rw-r--r-- 1 jano jano 0 Oct 24 22:04 annual report.pdf
Quoting values for reuse
Bash can produce a quoted form of any value.
s=$'it\'s a "test"\twith tab'
printf '%q\n' "$s"
echo "${s@Q}"
eval "t=${s@Q}"; [[ $t == "$s" ]] && echo "round trip OK"
printf '%q' and the ${var@Q} expansion return
a string that, read by the shell, gives the original value back. They
are useful for logging commands, generating scripts, and building remote
commands.
$ s=$'it\'s a "test"\twith tab'
$ printf '%q\n' "$s"
$'it\'s a "test"\twith tab'
$ echo "${s@Q}"
$'it\'s a "test"\twith tab'
$ eval "t=${s@Q}"; [[ $t == "$s" ]] && echo "round trip OK"
round trip OK
Quoting rules summary
| Form | Expands | Use for |
|---|---|---|
'text' | Nothing | Literal text, programs for awk, sed, grep |
"text" | $var, $( ), $(( )) | Any text with variables; the default |
$'text' | Backslash escapes | Tabs, newlines, control characters |
\c | Nothing (one character) | Single special characters |
| unquoted | Everything, plus splitting and globbing | Patterns, deliberate splitting |
Best practices
- Double-quote every expansion:
"$var","$(command)","${array[@]}". - Use "$@" to pass arguments on: Never
$*or$@unquoted. - Single-quote programs and patterns: For
awk,sed,grep, andfind -name. - Store argument lists in arrays: Not in strings with embedded quotes.
- Pass values as arguments to sh -c: Do not paste them into the command string.
- Check scripts with ShellCheck: It reports most missing quotes.
Source
Bash manual: Quoting, Greg's Wiki: Quotes
In this article, we have used single quotes, double quotes, the
backslash, and ANSI-C quoting, seen how unquoted expansions are split and
how "$@" differs from "$*", stored arguments in
arrays, quoted programs and patterns for other commands, and passed
values safely through ssh and bash -c. See also
the shell expansion,
Bash loops, and
echo articles.
Author
List all Linux tutorials.