What does if [[ $? -ne 0 ]]; mean in .ksh

Rahul sawant picture Rahul sawant · Nov 22, 2013 · Viewed 35.5k times · Source

I have a following piece of code that says if everything is executed mail a person if it fails mail the person with a error message.

if [[ $? -ne 0 ]]; then
  mailx -s" could not PreProcess files" [email protected]
else            
  mailx -s" PreProcessed files" [email protected]
fi
done

I am new to linux coding I want to understand what if [[ $? -ne 0 ]]; means

Answer

bishop picture bishop · Nov 22, 2013

Breaking it down, simple terms:

[[ and ]]

... signifies a test is being made for truthiness.

$?

... is a variable holding the exit code of the last run command.

-ne 0

... checks that the thing on the left ($?) is "not equal" to "zero". In UNIX, a command that exits with zero succeeded, while an exit with any other value (1, 2, 3... up to 255) is a failure.