The special variable “$?” returns the exit code of last executed command in Bash shell script. The following example gets the exit code of “grep” command whose exit code will be zero if finds match and 1 otherwise.

#!/bin/bash
grep neo users.txt
if [ "$?" == "0" ]; then
   echo User neo detected
else
   echo User neo not found
fi