Bash - (Return|Exit) (Value|Status)

About

The return status (also known as Exit Status of Exit Code) of a simple command is its exit status as provided by:

  • the posix 1003.1 waitpid function,
  • or 128+n if the command was terminated by signal n.

Management

Get

? is a special parameter that expands to the status of the most recently executed foreground pipeline.

echo $?
0

Ignore

/usr/bin/somecommand || /bin/true

Snippet

Basic

RESULT=$?
if [ ${RESULT} -ne 0 ];
then
    echo -e "\nLast command has failed."
else
    echo -e "\nLast command has succeeded."
fi 

One liner

[ "$?" -eq 0 ] && echo success  || echo failure

Powered by ComboStrap