Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move from [ ] to [[ ]] #27

Open
charles-dyfis-net opened this issue Jun 13, 2013 · 1 comment
Open

Move from [ ] to [[ ]] #27

charles-dyfis-net opened this issue Jun 13, 2013 · 1 comment

Comments

@charles-dyfis-net
Copy link

The [[ ]] test syntax is both more flexible and less error-prone:

  • Does not require parameter expansions on the left-hand side to be quoted.
  • Always treats expansions to empty strings as "" rather than string-splitting them out of existence (and thus potentially causing syntax errors).
  • Allows pattern tests by using an unquoted glob expression on the right-hand side of an = test. ([[ $foo = 'Hello '* ]])
  • Allows regexp tests by using an unquoted regular expression on the right-hand side of an =~ test. For maximum flexibility and compatibility across versions, the regex should be in a variable: re='foo.*bar'; [[ $var =~ $re ]]. Stores groups within matched regexes to the ${BASH_REMATCH[@]} array.
  • Allows substring tests by using a quoted value on the right-hand side of an =~ operator: [[ $foo =~ "$substring" ]] determines whether the literal contents of the $substring variable exist anywhere within $foo.
  • Allows standard grouping operators and short-circuiting boolean logic within the test itself, as so: [[ a = b && ( c = d || e = f ) ]]
@hugoduncan
Copy link
Member

The better behaviour around quoting and empty expansions alone makes this worthwhile.

Taking advantage of the additional capabilities is going to require a little more effort:

  • We need a way to express the glob, regex and substring matches
  • To use the grouping and logic operators, we need to recognise test expressions at a compound level. Currently test expressions are recognised at a simple expression level.

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

No branches or pull requests

2 participants