grep exclude grep from output

When you run a Linux command and pipe it through grep, the grep text itself is shown in the output as well. Once common technique is to use “grep -v” to exclude it. But here is a handy tip which excludes the grep text by placing the first character in a parenthesis –

Normal grep output – notice “grep ssh” is shown in the output :

[daniel@kauai tmp]$ ps aux |grep ssh
root      2795  0.0  0.0  66236  1236 ?        Ss   Mar01   0:38 /usr/sbin/sshd
daniel    6317  0.0  0.0 103320   832 pts/1    S+   23:21   0:00 grep ssh
root     25544  0.0  0.0 100016  4232 ?        Ss   22:17   0:00 sshd: daniel [priv]
daniel   25552  0.0  0.0 100016  2008 ?        S    22:17   0:00 sshd: daniel@pts/8

With the parenthesis trick we can exclude “grep ssh” from the output –

[daniel@kauai tmp]$ ps aux |grep [s]sh
root 2795 0.0 0.0 66236 1236 ? Ss Mar01 0:38 /usr/sbin/sshd
root 25544 0.0 0.0 100016 4232 ? Ss 22:17 0:00 sshd: daniel [priv]
daniel 25552 0.0 0.0 100016 2008 ? S 22:17 0:00 sshd: daniel@pts/8

[/bash]