I thought that variable expansions should always be quoted. Lately, I'm seeing more and more examples in reputable source that do not quote variables in certain instances. However, I can't see a clear rule that states when exactly that is the case.
An example, taken from the excellent answer to this question:
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-e|--extension)
EXTENSION="$2"
shift # past argument
shift # past value
;;
-s|--searchpath)
SEARCHPATH="$2"
shift # past argument
shift # past value
;;
-l|--lib)
LIBPATH="$2"
shift # past argument
shift # past value
;;
--default)
DEFAULT=YES
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
Why are $#
and $key
not quoted? There are many more examples in that answer. In fact, in the first example there it's tail -1 "$1"
and in the second it's tail -1 $1
.
shellcheck.net doesn't care if I write key="$1"
or key=$1
, but it does care when it's an argument to tail
.