Linux Command Reference ยท Concepts
Shell quoting pitfalls
The most common Bash quoting bugs and their fixes: unquoted variables, filenames with spaces, accidental globs, empty values, $* versus $@, and building commands safely.
Most shell scripts that "work on my machine" and fail elsewhere have the same root cause: a variable or command substitution that was not quoted. Unquoted expansions are split into words at whitespace and then globbed against file names. A file called my report.txt, an empty variable or a value containing * is enough to change what the command does.
The fix is a habit, not a trick: quote every expansion unless you can say why you want splitting. This page collects the recurring bugs, each with a runnable demonstration.
rm $file # bug: splits and globs rm -- "$file" # safe
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.
The recurring bugs
1. Unquoted variables. cp $src $dst breaks on spaces, and on an empty $dst silently runs cp $src with one argument. Write cp -- "$src" "$dst".
2. Unquoted [ ... ] operands. With x="", [ $x = yes ] becomes [ = yes ], a syntax error. Quote the operands, or use [[ $x = yes ]], which does not split.
3. $* versus $@. Unquoted, both split every argument on whitespace. "$*" joins everything into one string (separated by the first IFS character). "$@" is the one you almost always want: one word per original argument, spaces preserved.
4. Commands stored in strings. cmd='ls -l "my dir"'; $cmd passes the literal quote characters as part of the arguments. Quotes inside a variable are data, not syntax. Use an array (cmd=(ls -l "my dir"); "${cmd[@]}") or a function.
5. Dangerous file names. A name that starts with - is read as an option: rm -rf style surprises. Put -- before names, or prefix a path with ./. Names can contain newlines too, so use find -print0 | xargs -0, or while IFS= read -r -d ''.
6. Wrong kind of quote. Single quotes block $var; double quotes allow it. echo '$HOME' prints the text $HOME. To include a single quote inside single quotes, close, escape and reopen: 'it'\''s', or use $'it\'s'.
7. Tilde and braces in quotes. "~/file" and "{a,b}" stay literal because quoting disables tilde and brace expansion. Use "$HOME/file".
8. Quoted globs that should expand. ls "*.txt" looks for a file literally named *.txt. Leave the star outside the quotes: ls "$dir"/*.txt.
9. Regex on the right of =~. In [[ $s =~ "$re" ]] a quoted pattern is matched literally. Put the regex in an unquoted variable: re='^a+b$'; [[ $s =~ $re ]].
10. Command substitution eats newlines. "$(cmd)" removes trailing newlines. If they matter, append a sentinel: out=$(cmd; echo x); out=${out%x}.
Usage notes
- When not to quote: the right side of an assignment (
a=$bdoes not split), the word aftercase, and inside[[ ]]on the left of==/=~. Quoting there is harmless but not needed. - Intentional splitting: set
IFSlocally and use an array:IFS=, read -ra items <<< "$csv". set -fturns off globbing, which helps when splitting data you do not trust, but quoting is the real answer.- ShellCheck finds nearly all of these (SC2086 is the unquoted-variable warning). Run it on every script.
- Quote in functions as well:
"$1","${arr[@]}","$(dirname "$0")". Nested quotes inside$( )are independent of the outer ones. - Quoting is not escaping for another language. Building an SQL string or an
ssh host "cmd $var"string needs that language's escaping; useprintf %qfor the shell.
Related
- Quoting, Word splitting, Order of expansions, Globbing, Safe scripting
xargs,printf- Lessons: Quoting, Arguments