Skip to content

unassigned-variable

Shellcorrectnessno fixdefault: offshell/shellcheck

Reports a lower case variable that the file reads and never assigns, reads with read, getopts, local or a for loop, or guards with a default (${name:-x}). Names with an upper case letter are left alone: they are the environment’s.

A variable that is read and never set is usually a misspelling of another one, and it expands to nothing without an error, so rm -rf "$dir/"* with dir misspelled becomes rm -rf /*.

To fix, correct the name, or assign the variable.

Reported
used=1
...
echo "$used" "$status" "${output:-}" "${missing[@]}"
local_var() {
...
local -a list=(a b)
...
arr[0]=x
echo "${arr[0]}" "${#arr[@]}" "$#" "$@" "$1" "$_"
cat <<EOF
$heredoc_read ${FROM_ENV}
EOF
echo '$single' "\$escaped"
while getopts "ab:" opt; do
...
shell:
lint:
rules:
unassigned-variable: warn # off (default), warn, error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line unassigned-variable in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
shellcheck SC2154 partial Lower case names only, reported at the first read.