Skip to content

Files

Latest commit

 

History

History
61 lines (40 loc) · 1.21 KB

SC2154.md

File metadata and controls

61 lines (40 loc) · 1.21 KB

Pattern: Lowercase variable is referenced but not assigned

Issue: -

Description

Double check that the variable is indeed assigned, and that the name is not misspelled.

Note: This message only triggers for variables with lowercase characters in their name (foo and kFOO but not FOO) due to the standard convention of using lowercase variable names for unexported, local variables.

Example of incorrect code:

var=name
n=42
echo "$var_$n.jpg"   # overextended

or

target="world"
echo "hello $tagret"  # misspelled

or

echo "Result: ${somecmd -a somefile}"  # trying to execute commands

Example of correct code:

var=name
n=42
echo "${var}_$n.jpg"

or

target="world"
echo "hello $target"

or

echo "Result: $(somecmd -a somefile)"

Exceptions

ShellCheck does not attempt to figure out runtime or dynamic assignments like with source "$(date +%F).sh" or eval var=value.

If you know for a fact that the variable is set, you can use ${var:?} to fail if the variable is unset (or empty), or explicitly initialize/declare it with var="" or declare var.

Further Reading