2
m@m-VirtualBox:~$ man netstat | grep "-t"
grep: invalid option -- 't'
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

I don't want to pass the -t option to grep. Instead I want to locate the line(s) of the man page of netstat that talk about the -t option of netstat if there are any.

I thought that enclosing the -t in quotes would be enough to make grep interpret it as a pattern and not as an option, but I was mistaken.

How to do what I want to do?

gaazkam
  • 1,410

2 Answers2

5

You can signal the end of options with a double dash --.

In this case:

man netstat | grep -- -t
4

I'll give you three options:

  1. Use -e to explicitly mark it as a pattern: grep -e -t
  2. Use -- to mark the end of options: grep -- -t (Caveat: recognizing -- as meaning end-of-options is recommend, but not strictly required, by the POSIX standard, so it might not be completely portable.)
  3. Change the pattern so it doesn't look like an option: grep "[-]t" (Caveats: here, you do want to quote it, so the shell doesn't treat it as a filename wildcard. Also, this won't work for fixed-string searches with grep -F or fgrep.)