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

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - 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.
Advertising

3 - Management

3.1 - Get

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

echo $?
0

3.2 - Ignore

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

4 - Snippet

4.1 - Basic

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

4.2 - One liner

[ "$?" -eq 0 ] && echo success  || echo failure
lang/bash/exit_status.txt · Last modified: 2019/03/12 13:51 by gerardnico