When command expansions are unquoted, word splitting and globbing will occur. This often manifests itself by breaking when filenames contain spaces.
Trying to fix it by adding quotes or escapes to the data will not work. Instead, quote the command substitution itself.
If the command substitution outputs multiple pieces of data, use a loop instead.
Exceptions
In rare cases you actually want word splitting, such as in
gcc $(pkg-config --libs openssl) client.c
This is because pkg-config outputs -lssl -lcrypto, which you want to break up by spaces into -lssl and -lcrypto. An alternative is to put the variables to an array and expand it:
args=( $(pkg-config --libs openssl) )
gcc "${args[@]}" client.c
The power of using an array becomes evident when you want to combine, for example, the command result with user-provided arguments:
Use find instead of ls to better handle non-alphanumeric filenames.
Problematic code:
ls-l|grep" $USER "|grep'\.txt$'
Correct code:
find.-maxdepth1-name'*.txt'-user"$USER"
Rationale:
ls is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read.
Here's an example:
$ ls-l
total 0
-rw-r----- 1 me me 0 Feb 520:11 foo?bar
-rw-r----- 1 me me 0 Feb 52011 foo?bar
-rw-r----- 1 me me 0 Feb 520:11 foo?bar
It shows three seemingly identical filenames, and did you spot the time format change? How it formats and what it redacts can differ between locale settings, ls version, and whether output is a tty.
ls can usually be substituted for find if it's the filenames you're after.
If trying to parse out any other fields, first see whether stat (GNU, OS X, FreeBSD) or find -printf (GNU) can give you the data you want directly.
Exceptions:
If the information is intended for the user and not for processing (ls -l ~/dir | nl; echo "Ok to delete these files?") you can ignore this error with a [[directive]].
{ is only recognized as the start of a command group when it's a separate token.
If it's not a separate token, like in the problematic example, it will be considered a literal character, as if writing "{echo" with quotes, and therefore usually cause a syntax error.
This {/} is literal. Check expression (missing ;/\n?) or quote it.
Problematic code:
rmf(){rm-f"$@"}
or
evalecho\${foo}
Correct code:
rmf(){rm-f"$@";}
and
eval"echo \${foo}"
Rationale:
Curly brackets are normally used as syntax in parameter expansion, command grouping and brace expansion.
However, if they don't appear alone at the start of an expression or as part of a parameter or brace expansion, the shell silently treats them as literals. This frequently indicates a bug, so ShellCheck warns about it.
In the example function, the } is literal because it's not at the start of an expression. We fix it by adding a ; before it.
In the example eval, the code works fine. However, we can quiet the warning and follow good practice by adding quotes around the literal data.
ShellCheck does not warn about {}, since this is frequently used with find and rarely indicates a bug.
Exceptions
This error is harmless when the curly brackets are supposed to be literal, in e.g. awk {'print $1'}. However, it's cleaner and less error prone to simply include them inside the quotes: awk '{print $1}'.
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
To read lines rather than words, pipe/redirect to a 'while read' loop.
Problematic code:
forlinein$(catfile|grep-v'^ *#')
do
echo"Line: $line"
done
Correct code:
grep-v'^ *#'<file|whileIFS=read-r line
do
echo"Line: $line"
done
or without a subshell (bash, zsh, ksh):
whileIFS=read-r line
do
echo"Line: $line"
done<<(grep-v'^ *#'<file)
or without a subshell, with a pipe (more portable, but write a file on the filesystem):
mkfifo mypipe
grep-v'^ *#'<file> mypipe &
whileIFS=read-r line
do
echo"Line: $line"
done< mypipe
rm mypipe
Rationale:
For loops by default (subject to $IFS) read word by word. Additionally, glob expansion will occur.
Given this text file:
foo *
bar
The for loop will print:
Line: foo
Line: aardwark.jpg
Line: bullfrog.jpg
...
The while loop will print:
Line: foo *
Line: bar
Exceptions
If you want to read word by word, you should still use a while read loop (e.g. with read -a to read words into an array).
Rare reasons for ignoring this message is if you don't care because your file only contains numbers and you're not interested in good practices, or if you've set $IFS appropriately and also disabled globbing.
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Can't follow non-constant source. Use a directive to specify location.
Problematic code:
."$(find_install_dir)/lib.sh"
Correct code:
# shellcheck source=src/lib.sh
."$(find_install_dir)/lib.sh"
Rationale:
ShellCheck is not able to include sourced files from paths that are determined at runtime. The file will not be read, potentially resulting in warnings about unassigned variables and similar.
Use a [[Directive]] to point shellcheck to a fixed location it can read instead.
Exceptions:
If you don't care that ShellCheck is unable to account for the file, specify # shellcheck source=/dev/null.
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
This {/} is literal. Check expression (missing ;/\n?) or quote it.
Problematic code:
rmf(){rm-f"$@"}
or
evalecho\${foo}
Correct code:
rmf(){rm-f"$@";}
and
eval"echo \${foo}"
Rationale:
Curly brackets are normally used as syntax in parameter expansion, command grouping and brace expansion.
However, if they don't appear alone at the start of an expression or as part of a parameter or brace expansion, the shell silently treats them as literals. This frequently indicates a bug, so ShellCheck warns about it.
In the example function, the } is literal because it's not at the start of an expression. We fix it by adding a ; before it.
In the example eval, the code works fine. However, we can quiet the warning and follow good practice by adding quotes around the literal data.
ShellCheck does not warn about {}, since this is frequently used with find and rarely indicates a bug.
Exceptions
This error is harmless when the curly brackets are supposed to be literal, in e.g. awk {'print $1'}. However, it's cleaner and less error prone to simply include them inside the quotes: awk '{print $1}'.
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...
Double quote to prevent globbing and word splitting.
Problematic code:
echo$1
foriin$*;do:;done# this done and the next one also applies to expanding arrays.
foriin$@;do:;done
Correct code:
echo"$1"
foriin"$@";do:;done# or, 'for i; do'
Rationale
The first code looks like "print the first argument". It's actually "Split the first argument by IFS (spaces, tabs and line feeds). Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result."
The second one looks like "iterate through all arguments". It's actually "join all the arguments by the first character of IFS (space), split them by IFS and expand each of them as globs, and iterate on the resulting list". The third one skips the joining part.
Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such.
Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one:
When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: "$HOME/$dir/src/*.c" will not expand, but "$HOME/$dir/src"/*.c will.
Note that $( ) starts a new context, and variables in it have to be quoted independently:
echo"This $variable is quoted $(but this $variable is not)"
echo"This $variable is quoted $(and now this "$variable" is too)"
Exceptions
Sometimes you want to split on spaces, like when building a command line:
options="-j 5 -B"
make$optionsfile
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
options=(-j 5 -B)# ksh: set -A options -- -j 5 -B
make"${options[@]}"file
or a function (POSIX):
make_with_flags(){make-j5-B"$@";}
make_with_flags file
To split on spaces but not perform glob expansion, Posix has a set -f to disable globbing. You can disable word splitting by setting IFS=''.
Similarly, you might want an optional argument:
debug=""
[[$1=="--trace-commands"]]&&debug="-x"
bash$debug script
Quoting this doesn't work, since in the default case, "$debug" would expand to one empty argument while $debug would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value:
debug=""
[[$1=="--trace-commands"]]&&debug="yes"
bash${debug:+"-x"} script
This is better than an unquoted value because the alternative value can be properly quoted, e.g. wget ${output:+ -o "$output"}.
As always, this warning can be [[ignore]]d on a case-by-case basis.
this is especially relevant when BASH many not be available for the array work around.
For example, use in eval or in command options where script has total control of the variables...