Skip to content
Nathan Arthur edited this page Jul 29, 2021 · 4 revisions

Brace expansions and globs are literal in assignments. Quote it or use an array.

Problematic code:

foo={1..9}
echo "$foo"
foo="/some/path/*"
echo "$foo"

Correct code:

foo=( {1..9} )
echo "${foo[@]}"
foo=(/some/path/*)
echo "${foo[@]}"

Note that either of these will trigger SC3030 ("In POSIX sh, array references are undefined") if you are using sh and not e.g. bash.

Rationale:

echo *.png {1..9} expands to all png files and numbers from 1 to 9, but var=*.png or var={1..9} will just assign the literal strings '*.png' and '{1..9}'.

To make the variable contain all png files or 1 through 9, use an array as demonstrated.

If you intended to assign these values as literals, quote them (e.g. var="*.png").

Exceptions

None.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally