Use set -u to avoid accessing uninitialized variables#8652
Use set -u to avoid accessing uninitialized variables#8652headius merged 2 commits intojruby:masterfrom
Conversation
A previous version of this script was incorrectly assuming that
an uninitialized variable would be initialized to a "false" value,
but uninitialized variables typically evaluate to a blank string
which is considered true. As a result of this, no-argument calls
behaved improperly, breaking the ability to pipe code into the
jruby command.
This commit enables `set -u` for the JRuby launcher script, to
trigger an error whenever an uninitialized variable is accessed.
Variables we expect to possibly be uninitialized (such as various
JAVA variables used to override the location of the java command)
have been modified to use the ${variable-} format that does not
trigger an error.
At least one IRB spec clears env before running a subprocess, and POSIX does not require $HOME, so avoid using it if unset.
mrnoname1000
left a comment
There was a problem hiding this comment.
In personal scripts I prefer to test that a variable is set with [ -z ${var+x} ] which is faster because it expands to either [ -z x ] or [ -z ] (which technically tests that -z isn't an empty string). There are performance implications of testing large variables with [ ] but probably not significant enough to overrule stylistic choices.
That's clever but I fear having to explain it to anyone new to shell. I suppose it is cleaner, since you are not actually consuming the contents of the variable there. Maybe we'll do it later. |
|
We could maintain quotes and use |
|
@mrnoname1000 Sure, we can do that as a separate PR. Probably should include at least a little doco to explain that pattern. |
A previous version of this script was incorrectly assuming that an uninitialized variable would be initialized to a "false" value, but uninitialized variables typically evaluate to a blank string which is considered true. As a result of this, no-argument calls behaved improperly, breaking the ability to pipe code into the jruby command.
This commit enables
set -ufor the JRuby launcher script, to trigger an error whenever an uninitialized variable is accessed. Variables we expect to possibly be uninitialized (such as various JAVA variables used to override the location of the java command) have been modified to use the${variable-}format that does not trigger an error.