Shellcheck: Enhancement: warn about undefined variables

Created on 16 Apr 2015  路  4Comments  路  Source: koalaman/shellcheck

Hello
Some time ago I asked for this feature and you've implemented SC2034 which covers many of these problem caused by typos. Anyway it still happens sometimes e.g. that I use $N0P instead of $NOP and there is no warning that $N0P is never defined. Sure there can be lots of false positive, but it would be helpful to have the ability of catching this problem.
A totally different approach to seek for such errors would be possible if there would be a statistic output of shellcheck which will show the variables used in a script.
e.g. when I ran:
shellcheck --stats foo.sh
I'd like to see an output table like:

var1,1,4
var2,3,1
...
where varX is the name of the variable, the first number shows how often this variable is set and the second number shows how often this variable is used.
Regards
Helmut

Most helpful comment

I think ShellCheck really needs a pedantic mode where it warns about things that it would otherwise find acceptable, like undefined uppercase variables. Would that eliminate the need for specific statistics?

All 4 comments

I think ShellCheck really needs a pedantic mode where it warns about things that it would otherwise find acceptable, like undefined uppercase variables. Would that eliminate the need for specific statistics?

Yes that would also be fine :)

It would be very nice to have an option to turn on warnings about undefined variables. I know some JavaScript linters (jshint comes to mind) do this and also they also have whitelists for variables than are intentionally undefined. Doing something similar in shellcheck would be really nice. Having a whitelist or maybe even reading variables from the environment would make using this option much less painful than it would otherwise be.

On Wed, 9 Aug 2017, Trevor Brown wrote:

It would be very nice to have an option to turn on warnings about undefined variables. I know some JavaScript linters (jshint comes to mind) do this and also they also have whitelists for variables than are intentionally undefined. Doing something similar in shellcheck would be really nice. Having a whitelist or maybe even reading variables from the environment would make using this option much less painful than it would otherwise be.

If you stick to the mode of using lower-case for unexported variables,
Shellcheck most certainly WILL tell you about them.

The assumption is that upper-case are either exported before the script
starts, or built shell. (Or set explicitly, of course, but then this query
is moot.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

balloonpopper picture balloonpopper  路  4Comments

arth1 picture arth1  路  4Comments

bbarker picture bbarker  路  3Comments

mechalynx picture mechalynx  路  5Comments

szepeviktor picture szepeviktor  路  4Comments