Why the following if statement succeeds ?
if $(ps aux | grep -q "bla bla") ; then echo "found" ; fi
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the
grepprocess itself is being returned byps. You can “trick”grepto not match itself by surrounding one of the search characters in a character class[ ]which doesn’t change the functionality:Just do:
Also, the use of process substitution
$()is unnecessary. Theifwill 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
psoutput still has the character class brackets but whengrepis processing the search string, it uses the brackets as syntax rather than a fixed string to match.