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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…