exit status of the script

Every shell script returns a value.

When it returns 0 value, it means that script was run successfully.
But if it returns non-zero value, it means that script did not run as expected.Non-zero value actually indicates the error code.

You can use “exit” command to return a value from the script.

Both of the below commands are equal.

exit
exit $?

You can even omit “exit” statement at the end of script as it is added implicitly by BASH.

If you use pipes, then $? will give you the result of only last command in the pipe. To get the exit status of every command, you can use PIPESTATUS variable.

$ false | true ; echo ${PIPESTATUS[0]}

$ false | true | false; echo ${PIPESTATUS[*]}

When script or command is successful, it returns 0. But since the command is successful, it is considered to be true when checking the condition.

When script or command fails, it returns non zero value (1). But since the command has failed, it is considered to be false when checking the condition.

You may also like...