This uses rules explained in the Pattern Matching section of man bash
:
[[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]]
This uses regular expressions to perform the matching:
[[ $OSTYPE =~ (darwin|freebsd|solaris|cygwin|openbsd) ]]
These are very different mechanisms, with different performance implications. There are no patterns in the values darwin
, freebsd
, ..., these are simple literal strings. The @(...)
syntax is perfect for such simpler matching logic, so using regular expressions seems overkill.
In this example both writing styles give the same behavior, it's just that they perform the matching through different mechanisms. If instead of a list of literal strings like darwin
, freebsd
, ... you had more complex regex patterns, then the first writing style wouldn't be an option, you would need to use the second version with full regex power.
Is this syntax used in place of the =~
operator for better portability with regex ?
It is used because it is good enough for the purpose, regex not needed.