Placing -- ahead of the pattern is the defacto standard way to indicate end of command line flags. Most commands that expect dashed command line flags recognise the double dash with trailing whitespace to mean the end of the list and to assume the next token is the first positional parameter.
Using the [-]- obviously works, but makes the intent of the pattern less obvious.
I was going to quote the grep man page, but it makes no mention of this.
The thing is that if I bracket --help, grep does not run the --help command line option.
echo "--help" | grep -iE '(--help)'
But doing
echo "--help" | grep -iE '--help'
does run the help option of grep.
Using the brackets did not require use of [-]- or -- in front of the pattern.
Correct me if I am wrong on the following:
In the regular expression [-]-help, the - inside the square brackets matches a character set which contains a single character ( - in this case) and the literal string "-help".
If I were putting this into a script I'd code it this way:
echo "--help" | grep -iE -- '--help'
Extra spaces aren't needed, just set the pattern off a bit. The first pair of dashes end greps search for flags, and thus it treats --help as the pattern.
---------- Post updated at 21:36 ---------- Previous update was at 21:34 ----------
Of interest may be the fact that POSIX requires compliant echo implementations to NOT support the -- argument's semantics. echo
This means that conformant echos cannot support any command options. Of course, in practice, most do implement some options and there's usually no way to disable the option processing. This renders the general case of echo $text problematic. If $text expands to a valid option, the result is something unintended.
This is why these days I use printf for most everything.