Script breaks when using bash's nounset option #15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Optparse breaks when
set -o nounset
is enabled. This option makes bash generate an error when an uninitialized variable is encountered. Having this option on is generally good practice, kinda like strict mode, and is easy to fix.Example code
Create an option with no default, turn on set -o nounset, and don't specify a value for argument.
Now run the script with no arguments:
Expected Result
Should not throw an error
Cause
Local variables in the optparse.build parser loop are not declared. So since no 'default' attribute was given in optparse.define, it's uninitialized when we check for it later.
Also when generating $optparse_defaults, arguments with no default specified should be declared empty.
Fix
Easy fix, just initialize all the local variables in optparse.define, prior to the parsing loop. Also in $optparse_defaults, when $default is empty, declare an empty variable.
Above for loop
While generating $optparse_defaults
Final Thoughts
I've got a pull request coming your way to fix this too.
This is a really easy bug fix for an issue that violates best practices for bash scripting. I hope you'll accept it in a timely manner. Thanks.