Skip to content

Fixed plenv-rehash for older GNU Bash#124

Open
simonmeggle wants to merge 1 commit intotokuhirom:masterfrom
simonmeggle:dev
Open

Fixed plenv-rehash for older GNU Bash#124
simonmeggle wants to merge 1 commit intotokuhirom:masterfrom
simonmeggle:dev

Conversation

@simonmeggle
Copy link

plenv shims are only executable on older GNU bash versions (e.g. 3.1.17) when the regex is enclosed within quotes.

@syohex
Copy link
Collaborator

syohex commented Mar 19, 2016

Does this change work for newer bash ?

Behaviour of Bash 3.2 or higher version is here

f.  Quoting the string argument to the [[ command's  =~ operator now forces
    string matching, as with the other pattern-matching operators.

See also

@ghost
Copy link

ghost commented May 8, 2017

No, it does not; this commit would result in breakage.

For maximum cross-compatibility between the various bash versions, assign the regex to a variable first, then expand it as the RHS of the =~ operator without quoting it. For example:

re='^(--$|-[0A-Za-z]*[eE].*)'
for arg; do
  [[ "\$arg" =~ $re ]] && break

Alternatively, use extglobs (at the cost of being unfamiliar to those that don't spend much time hacking in bash):

shopt -s extglob
for arg; do
  [[ "\$arg" == @(--|-*([0A-Za-z])[eE]*) ]] && break

EDIT: Also, the code seems broken to begin with. Why is $arg quoted and the sigil escaped? Consider this bad code:

$ echo $BASH_VERSION
4.3.48(1)-release
$ ( set -x; foo=123; [[ "\$foo" == 123 ]] && echo "matched" )
+ foo=123
+ [[ $foo == 123 ]]

Then compare it to this:

$ ( set -x; foo=123; [[ $foo == 123 ]] && echo "matched" )
+ foo=123
+ [[ 123 == 123 ]]
+ echo matched
matched

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants