Exit status of grep

I am trying to get the exit status of grep and test a condition with it, But it does not seem to be working as expected since i am doing something wrong apparently

as per grep help
Exit status is 0 if match, 1 if no match,
and 2 if trouble.

My problem is something like this

templine - a string which has isVoid()

isVoid=`echo $tempLine | grep -o "isVoid\(\)"`
                echo "isVoid: " $isVoid 
                if  [ $isVoid = 0 ]
                then
                    echo "Is Void matched" $isVoid
                fi

When I echo $isVoid, i get the matched string, but i am not sure how to test the exit status of grep. In this case the if condition is not satisfied

change the if condition as below...

if  [ $isVoid -eq 0 ]
then
   echo "Is Void matched" $isVoid
fi

= is an assignment operator

Try:

isVoid=`echo $tempLine | grep -o "isVoid\(\)"`
if [ $? -eq 0 ]
then
  echo Success
fi

If you grep for "isVoid\(\)" the output of grep should never be 0.

Mybe this works :

if isVoid=$(echo $tempLine | grep -o "isVoid\(\)")
then echo "Is Void matched" $isVoid
fi

$() command substitution is preferred to ``

Assigning the substituted output of a command to a variable will simply contain it's output, ie. the pattern grep -o found or not. It does not contain the return code of the grep!

Use Franklins example since it is the only one of that cares for the exit code which is checked by $? immediately after the command you want to observe.

It doesn't have anything to do with the type of how it is substituted ie. double `or $().

The variable contains the output but the return code corresponds to the return code of the last command (grep). Try to print $? and you'll see what happens.

Right, it just makes it more readable and permits the nesting.

This is what happens:

$ s=`echo 'abc def'|grep 'xxx'`
$ echo $?                      
1
$ s=`echo 'abc def'|grep 'abc'`
$ echo $?                      
0

Thanks a lot for the replies guys,.

I used the $? method and it works fine.