1

In my script I have the following:

$cmd arg1 arg2 >/dev/null 2>&1
if [ $? -eq 0 ]; then
   # cmd succeeds, do something
fi

Is there a way to make it shorter? I checked man test for various options in [] command, but could not find anything that could make it shorter.

Mark
  • 1,793

1 Answers1

4

You can act on the return of the command directly

if cmd arg1 arg2 >/dev/null 2>&1; then
  do-the-things
fi

If it returns 0 it will be true, anything other than 0 will be false.

You can read about conditional constructs in the bash manual

jesse_b
  • 37,005