skip to content
Alvin Lucillo

Escape hyphen in grep

/ 1 min read

💻 Tech

If you have a grep command with a search string that starts with a hyphen, you may encounter an error.


grep "-key=value" filename.yaml

# error:
# grep: invalid option -- 't'
# Usage: grep [OPTION]... PATTERNS [FILE]...
# Try 'grep --help' for more information.

To resolve this, you can use -- to tell the grep that what follows are not command options. The reason for this is the hyphen is interpreted as an introduction to an option.

grep -- "-key=value" filename.yaml