I run the following command in bash:
echo "#fastq-dump SRR3105676 --gzip -O my &" | egrep "-O"
and I get
grep: invalid option -- 'O'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.
I understand that to fix this I need to escape the hyphen (dash) in the quotes, but why is this happening? Why is the shell interpreting the "-O" as an option and not as a regex?
The shell isn't interpreting anything - it's simply passing
-O
(after quote removal) to thegrep
executable, which parses it as part of its argument vectorargv[]
You can either signal the end of options using
--
or (for the case of
grep
specifically) use the-e
option (or its long form--regexp
) to explicitly tell it that the next argument is an expression:From
man grep
: