When listing processes (via ps or other) and piping to grep for one particular process matching a regular expression, people will often pipe the output to 'grep -v grep' (-v option returns lines that don't match regular expression) to make sure grep doesn't find itself, as in the following:

$ ps ax | grep nslookup
26363 ttyp8    S      0:00 nslookup
26365 ttyp8    S      0:00 nslookup
26366 ttyp8    S      0:00 nslookup
26367 ttyp8    S      0:00 nslookup
26368 ttyp8    S      0:00 nslookup
31337 ttyp9    S      0:00 grep nslookup

What was expected was a listing of all 'nslookup's running on the system, but the actual grep for these processes found itself as a running process. Here's an example of grep -v grep in action:

$ ps ax | grep nslookup | grep -v grep
26363 ttyp8    S      0:00 nslookup
26365 ttyp8    S      0:00 nslookup
26366 ttyp8    S      0:00 nslookup
26367 ttyp8    S      0:00 nslookup
26368 ttyp8    S      0:00 nslookup

Some would argue this is a pointless use of grep.

If you've used grep -v grep recently, you should type man ps next and read up on the -C argument.

$ ps -C getty
  PID TTY          TIME CMD
  206 tty1     00:00:00 getty
  207 tty2     00:00:00 getty
  208 tty3     00:00:00 getty
  209 tty4     00:00:00 getty
  210 tty5     00:00:00 getty
  211 tty6     00:00:00 getty
This feature can also be avoided with a clever use of regular expressions. While grepping for a string will return the grep process as well, grepping for a regexp probably won't.

tcsh% ps ax | grep "[t]csh"

  850 pts/2    SW     0:00 tcsh
  851 pts/1    SW     0:03 tcsh
 1059 pts/3    SW     0:00 tcsh
 1170 pts/4    S      0:05 tcsh
22558 pts/10   S      0:03 tcsh
31554 pts/6    SW     0:00 tcsh
24027 pts/21   S      0:01 tcsh
31280 pts/23   S      0:01 tcsh
 6470 pts/5    S      0:00 tcsh

Log in or register to write something here or to contact authors.