HostOnNet Blog

Check if a command was successful

When you execute a program in linux command prompt or bash script, program return an exit code on completion.

Return value is available in special variable $?

When a program execute successfully, it return 0.

For example

Here first command pwd was successfully executed, so we get return value of 0.

Next command “pwd2” was not found, we got return value of 127.

By checking the value of $? after executing a program we can idetify if a program was successful or not.

0 Program executed successfully.
1 Catchall used for general errors.
2 Misuse of shell builtins.
126 Program cannot execute. Can be permission error.
127 Command not found. Missing binary.
128 Command not found. Missing binary.
128 + N This happens if a program get killed with kill command.
130 program cancelled by CTRL+C.

See bash

Posted in Linux