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.

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

See bash


Posted in Linux