I want to grep for the string that starts with a dash/hyphen, like -X
, in a file, but it's confusing this as a command line argument.
I've tried:
grep "-X" grep \-X grep '-X'
An alternative you can use instead of the double dash is -e . For example, grep -w -e -r (or grep -we -r ) would have the same result as grep -w -- -r in the example above. -e allows specifying a search pattern, and it can be used to protect a pattern beginning with a dash ( - ).
If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.
To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.
Use:
grep -- -X
Related: What does a bare double dash mean? (thanks to nutty about natty).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With