5 Answers
5

Because the grep process itself is being returned by ps. You can "trick" grep to not match itself by surrounding one of the search characters in a character class [ ] which doesn't change the functionality:
Just do:

if ps aux | grep -q "[b]la bla" ; then echo "found" ; fi

Also, the use of process substitution $() is unnecessary. The if will work on the success of the last command in the pipe chain, which is what you want.

Note: The reason the character class trick works is because the ps output still has the character class brackets but when grep is processing the search string, it uses the brackets as syntax rather than a fixed string to match.

If you grep the output from ps aux, you will always get a process showing your previous command. To fix this, you can pipe the output to grep twice, once to remove line with "grep" in it, and again for the process your looking for.

The $( is a small little bit relevant, and changes the meaning a bit. Although in this case, because there is never any output from grep -q, you can just about get away with the $(. You probably want to start with something like (as pointed out by others):

if ps aux | grep -v 'grep' | grep -q 'bla bla'; then
echo 'Found'
fi

Anyway, you started with

if $(ps aux | grep -q "bla bla") ; then echo "found" ; fi

With $(, the command inside the $( ) is executed and the output of that command is used as the command line for the outer command. Do these four experiments:

So, according to this you will output get found if both these conditions hold:

The command inside the $( ) created no output

and the command was succesful

This suggests that ps aux | grep -q "bla bla" was successful and created no output. It's no surprise that grep -q creates no output. That's what the -q is for. So therefore, your command must have had a true status, which implies that the grep did successfully find a match. We know that grep will always find a match in this case, because the list of processes from ps will include grep itself; the grep will always find itself.

don't you want the -v grep after the target grep? (If I downvote, I say why).
– shellterJan 22 '12 at 23:28

I also got downvoted twice with no explanation. I think someone is downvoting very liberally on this question ...
– laherJan 23 '12 at 0:31

@amir75 : looks liked all the downvotes have disappeared (again with no explanation!) ;-) Good luck to all.
– shellterJan 23 '12 at 0:48

@shellter: Normally, yes, I would filter after the grep, but since we're calling grep -q, there's nothing to filter. I don't follow the downvote comments (there are no votes on this answer).
– JohnsywebJan 23 '12 at 7:53