-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Labels
Description
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$substringvariable 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 ) ]]