Shell

Shell

Made by DeepSource

Unused variable SH-2034

Anti-pattern
Major

Variables that are declared but not used for anything should be removed.

Problematic code:

foo=42
echo "$FOO"

Preferred code:

foo=42
echo "$foo"

Exceptions:

This warning may be falsely emitted when a variable is referenced indirectly, or it is intentionally not used.

  • Indirection: It's the checker's intended behavior to emit this warning for any variable that is only referenced though indirection:
# foo generates a warning, even though it has five indirect references
foo=42
name=foo
echo "${!name} $((name))"
export "$name"; eval "echo $name"
declare -n name; echo "$name"

This is an intentional design decision and not a bug. If you have variables that will not have direct references, consider using an associative array in bash, or ignore this warning.

  • Intentionally unused variables: For throwaway variables, consider using _ as a dummy:
read _ last _ zip _ _ <<< "$str"
echo "$last, $zip"