Ubuntu – ps aux | grep with a dot

command linegrepps

I'm trying to write a script which greps for a running process.

I'm trying to avoid the actual grep from coming back from the ps aux output.

However I'm getting varying results if I use a dot or not:

ps aux | grep [s]elenium doesn't return anything.

ps aux | grep [s]elenium.jar returns the grep command:

beebee   36155  0.0  0.0  15940   956 pts/0    S+   16:20   0:00 grep --color=auto selenium.jar

Why is that?

Best Answer

I guess you have a file named selenium.jar but no file called selenium in your current folder.

If you run

 ps aux | grep [s]elenium.jar

the shell will try to replace [s]elenium.jar with matching files names from the current folder. If there is a file called selenium.jar that will match and [s]elenium.jar will be replaced by selenium.jar.

The shell will then execute the command with the replaced value, i.e.

ps aux | grep selenium.jar

To avoid this quote the regex to protect it from the shell:

ps aux | grep '[s]elenium.jar'
Related Question