A Grep Trick Every User Should Know: The Square Brackets in the Grepped ps Output

Mastering the command line involves more than just knowing the tools—it’s about using them smartly. One subtle but incredibly useful trick when working with grep and ps is using square brackets to filter processes without matching the grep command itself. This technique is especially helpful when you’re scripting or debugging and want clean output. If you’re interested in enhancing your terminal productivity further, check out this guide on smarter tab completion using .inputrc.

This method is a clever workaround to avoid the annoyance of seeing the grep process itself in the results when you’re searching for running processes. It’s a small trick with big practical value—an example of how small syntax tweaks in the shell can save time and improve clarity. This complements other CLI optimizations, like those in this list of 20 useful find command oneliners.

Why Use Square Brackets in grep with ps

When you use a command like:

ps -ef | grep bash

The output includes the grep command itself because it contains the string you’re searching for. So, even though you’re looking for bash, the line running grep bash also gets included in the results. Here’s an example output:

115     2524  2507  0 09:34 pts/0    00:00:00 bash
mike    2584  2576  0 09:34 pts/0    00:00:02 bash
mike    7412  7411  0 15:51 pts/1    00:00:01 -bash
mike    8518  8517  0 17:29 pts/2    00:00:02 -bash
mike   11039  8518  0 18:27 pts/2    00:00:00 grep --colour=auto bash

Note that the last line is the grep command itself. This can clutter your results, especially when you’re using grep in scripts or automated checks.

How Square Brackets Fix It

To avoid matching the grep process, use square brackets around the first letter of your search string:

ps -ef | grep [b]ash

The square brackets still match the letter b, so the pattern matches bash, but it prevents grep [b]ash from matching itself. Here’s what the cleaned-up output looks like:

115     2524  2507  0 09:34 pts/0    00:00:00 bash
mike    2584  2576  0 09:34 pts/0    00:00:02 bash
mike    7412  7411  0 15:51 pts/1    00:00:01 -bash
mike    8518  8517  0 17:29 pts/2    00:00:02 -bash

No more grep line at the bottom. This makes the output cleaner and eliminates the need for additional filtering with tools like awk or sed.

Real-World Example with ssh

Let’s consider another case with SSH processes:

ps -ef | grep ssh

Output:

root     445     1  0 09:23 ?        00:00:00 /usr/sbin/sshd -D
115     2317  2267  0 09:33 ?        00:00:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session gnome-session --session=gnome
root    7306   445  0 15:50 ?        00:00:00 sshd: mike [priv]
mike    7411  7306  0 15:51 ?        00:00:00 sshd: mike@pts/1
root    8412   445  0 17:29 ?        00:00:00 sshd: mike [priv]
mike    8517  8412  0 17:29 ?        00:00:01 sshd: mike@pts/2
mike   11003  8518  0 18:19 pts/2    00:00:00 grep --colour=auto ssh

Again, the grep line appears. Now let’s use brackets:

ps -ef | grep [s]sh

Output:

root     445     1  0 09:23 ?        00:00:00 /usr/sbin/sshd -D
115     2317  2267  0 09:33 ?        00:00:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session gnome-session --session=gnome
root    7306   445  0 15:50 ?        00:00:00 sshd: mike [priv]
mike    7411  7306  0 15:51 ?        00:00:00 sshd: mike@pts/1
root    8412   445  0 17:29 ?        00:00:00 sshd: mike [priv]
mike    8517  8412  0 17:29 ?        00:00:01 sshd: mike@pts/2

Much better! No more grep entry.

Why This Matters in Scripts

When automating tasks in scripts, it’s common to use ps and grep to check if a process is running. If the grep command itself appears in the results, it can cause false positives. For example:

if ps -ef | grep bash > /dev/null; then
    echo "Bash is running"
fi

This might always return true—even if bash isn’t running—because the grep bash line is always present. Using grep [b]ash avoids this problem and ensures your condition checks are accurate.

Conclusion

This simple trick using square brackets with grep can make your shell usage cleaner, more accurate, and more professional. It removes the noise from your terminal output and helps build better, more reliable scripts.

Next time you’re checking running processes, remember this technique. It’s a classic example of a tiny syntax adjustment that makes a big impact. And if you’re on a path to mastering the Linux CLI, don’t forget to check out advanced tips like find command oneliners and .inputrc customizations for faster navigation.

8 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *