The Bash reference section on conditional constructs says:
When the ‘==’ and ‘!=’ operators are used, the string to the right of the
operator is considered a pattern and matched according to the rules described
below in Pattern Matching, as if the extglob shell option were enabled. ...
An additional binary operator, ‘=~’, is available, with the same precedence as
‘==’ and ‘!=’. When it is used, the string to the right of the operator is
considered an extended regular expression and matched accordingly (as in regex
3)).
But then I try this:
$ [[ -good == -* ]] ; echo $?
0
$ [[ -g == -* ]] ; echo $?
bash: syntax error in conditional expression
bash: syntax error near `-*'
$ [[ -g == -? ]] ; echo $?
bash: syntax error in conditional expression
bash: syntax error near `-?'
Then I try the regex matching operator =~
:
$ [[ -good =~ -.* ]] ; echo $?
0
$ [[ -g =~ -.* ]] ; echo $?
bash: syntax error in conditional expression
bash: syntax error near `-.*'
$ [[ -g =~ -.? ]] ; echo $?
bash: syntax error in conditional expression
bash: syntax error near `-.?'
Why the syntax errors?