Is there are way to check that all variables declared in a bash script are being used? Something analogous to -Wall in c++?
Cheers
Is there are way to check that all variables declared in a bash script are being used? Something analogous to -Wall in c++?
Cheers
There's no built-in way to do this, no, and it can never be done perfectly, because of situations like these:
CLASSPATH as an environment variable. Then a Bash script might include a statement like CLASSPATH=...but never refer to
CLASSPATH, if its only purpose in assigning to CLASSPATH is to modify the behavior of some program that uses the environment variable (such as java).a and b, and a variable c whose value is obtained from the user and can be either a or b. I can then use ${!c} to obtain the value of the user-specified variable; a given run of the script might never refer to b (because c is set to 'a'), but a different run of the script might do differently.That said, you might be interested in the -u option to the set builtin. If your script contains this command:
set -u
then from that point on, it will be an error to refer to a variable or parameter that has not been set. This can help detect typos in variable-names and whatnot. This is obviously much less than what gcc -Wall does (since gcc always gives an error-message when you refer to an undeclared variable), but you may find it beneficial in the same way.
set -u would be useful. However, I'm looking for something that would check all declared variables are being used, not that all variables that are being used are declared. Perhaps a bash script would be able to do this?