Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created October 12, 2023 16:54
Show Gist options
  • Save michabbb/ade2a9650628783d1aba4fed4d4f78d2 to your computer and use it in GitHub Desktop.
Save michabbb/ade2a9650628783d1aba4fed4d4f78d2 to your computer and use it in GitHub Desktop.
ps ax grep without showing your grep ;)
> ps ax|grep sleep
output:
954190 ? Ss 0:00 sleep infinity
1036329 ? S 0:00 sleep 5
1036386 ? S 0:00 sleep 2
1036397 pts/4 S+ 0:00 grep --color=always sleep <---- you don´t want to see this
3610265 ? S 0:00 sleep 1d
3868324 ? S 0:00 sleep 86400
that´s why, we use a little trick:
> ps ax|grep [s]leep
954190 ? Ss 0:00 sleep infinity
1037316 ? S 0:00 sleep 5
1037353 ? S 0:00 sleep 2
3610265 ? S 0:00 sleep 1d
3868324 ? S 0:00 sleep 86400
why does this work ?
The square brackets around the first letter of the command, [s]leep,
create a character class that matches either the letter "s" or the letter "l".
This ensures that the grep process itself is not matched, as it contains the
string "sleep" in its command line.
@michabbb
Copy link
Author

in case chatgpt is correct and you are a mac user, try this:


The command you're using employs a technique to filter out the grep command itself from the results of grep. The square bracket trick, as in [s]leep, is indeed a commonly used approach in Linux.

The issue you're experiencing on macOS isn't related to the grep command or the square brackets themselves. The actual problem lies with the zsh shell, which is the default shell in newer versions of macOS.

The zsh interprets the square brackets as a pattern for filenames. If it can't find a matching file, it returns the "no matches found" error.

To get around this, you have two main options:

  1. Enclose the square brackets in quotes:

    ps ax | grep "[s]leep"

  2. Disable the nomatch option in zsh:

    You can disable this option temporarily for your current session with:

    setopt no_nomatch

    If you want to do this permanently for all zsh sessions, you can add the above line to your .zshrc file.

Either approach should resolve the issue and allow you to use the grep command with square brackets on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment