0

In grep, when I try to find a pattern in the man page, the pattern is treated as the option of grep if it starts with -. For example, I find someone is using uname -r and I wonder what does -r means for uname.

 uname --help | grep '-r'

But I got the following error,

Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information.

Seems as if -r is treated as an option for grep.

Peng Zhang
  • 3,475
  • 4
  • 33
  • 41

3 Answers3

4

There are two ways:

  1. If supported by your version, use -- to stop argument processing.

    uname --help | grep -- '-r'
    
  2. Use the -e option to specify the pattern. Since it expects an argument, grep will not treat the next string as a new option just because it starts with a hyphen.

    uname --help | grep -e '-r'
    
chepner
  • 497,756
  • 71
  • 530
  • 681
4

As described in the man page for grep:

-e PATTERN, --regexp=PATTERN

Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX.)

uname --help | grep -e -r does what is expected.

gandaliter
  • 9,863
  • 1
  • 16
  • 23
1

Backslash also seems to help:

uname --help | grep "\-r"
uname --help | grep '\-r'
Vytenis Bivainis
  • 2,308
  • 21
  • 28