That's a
Useless Use of Test $?.
if already examines the exit code
$? from the command it executes, so it's simpler and more straightforward to do
Code:
if egrep -i "string" file_name
then
echo Found
else
return $?
fi
To negate an
if, use an exclamation mark.
Code:
if ! egrep -i "string" file_name
then
return $?
fi
There's another shorthand you should know about
: the "or" connective. It executes the second command if the first command fails.
Code:
egrep -i "string" file_name >/dev/null || return $?
There is also && "and" which operates the other way around.
(I added redirection to
/dev/null as
egrep is used simply for its return value here. You don't want it to actually print any matches. If your
egrep has the
-q option, you could use that too.)