Variable appears unused
SC2034: variable appears unused. Why ShellCheck flags it, when it is a real bug, and how to silence false positives with `# shellcheck disable=SC2034`.
Problem
Setting a variable and never reading it is usually dead code — a leftover from a refactor or, worse, a typo in the *consumer*. SC2034 flags the assignment so you investigate. The common false positive is a variable that's only consumed by an indirect mechanism (`set -a` then `source`, or `printf -v`). In that case, silence the warning explicitly with a directive.
Bad
version="1.2.3"
# ...rest of script never references $versionGood
# If truly unused: delete it.
# If referenced indirectly, document that and silence the warning:
# shellcheck disable=SC2034
version="1.2.3"Explanation
Always delete dead code first. Suppression via `# shellcheck disable` should be the last resort and should include a comment explaining why.
Related
SC2154
When It Matters
An assigned-but-unused variable is usually one of three things: a typo at the point of use, a leftover from deleted code, or a value that was meant to be exported and is not. All three are worth a look, and the last one is a real bug — a child process reading the environment will not see a variable that was only assigned locally. In long scripts the warning is also a cheap way to find dead configuration that people are still maintaining.
Second Example
Note
Naming a deliberately unused variable _ documents the intent and stops the warning at the same time.
Exceptions
Variables consumed by a sourced file, referenced indirectly through ${!name}, or read by an external tool that parses the script are all genuinely used but invisible to static analysis. Mark those with "# shellcheck disable=SC2034" and a one-line note about who reads them.
Faq
Q
Does export make the warning go away?
A
Yes, because an exported variable is observably used by child processes even if the script itself never reads it again.
Q
What about variables used only in a sourced file?
A
Add a source directive so ShellCheck can follow the file, or disable the warning at the assignment with a comment naming the consumer.
Q
Should I delete every flagged variable?
A
Check the point of use first. A surprising share of these warnings are typos in the reference rather than genuinely dead assignments.