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,400
  • 3
  • 10
  • 17
  • 1
    See also [-e option in command grep](https://unix.stackexchange.com/questions/435412/e-option-in-command-grep) – steeldriver Jul 18 '21 at 19:54
  • Also: https://unix.stackexchange.com/questions/388892/match-with-grep-when-pattern-contains-hyphen – muru Jul 19 '21 at 04:54

2 Answers2

5

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

In this case:

man netstat | grep -- -t
Eduardo Trápani
  • 12,032
  • 1
  • 18
  • 35
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.)
Gordon Davisson
  • 4,360
  • 1
  • 18
  • 17