Linux Bash Exit Codes Explained | Exit Codes in Linux Bash

Linux Bash Exit Codes Explained

What is an Exit Codes in Linux Bash

An exit code, or sometimes known as a return code, is the code returned to a parent process by an executable.

Every script, command, or binary exits with a return code.

Just type $? and press enter and you will see the exit code of the previous command.

Exit code “0” or zero denotes success of the command.

Bash has some reserved exit codes for different situations. So, you should avoid using reserved exit codes as it will make debugging harder because of conflicting results. Return codes are numeric and are limited to being between 0-255. If you use a value of -1, it will return 255. Each execution terminates with an exit code, whether successful or not, with an error message or silently.

Below table details the reserved exit codes:

Exit Code NumberDescriptionExampleComments
1Catchall for general errorslet “var1 = 1/0” Miscellaneous errors, such as “divide by zero” and other impermissible operations
2Misuse of shell builtins empty_function() {}Missing keyword or command
126Command invoked cannot execute/dev/nullPermission problem or command is not an executable
127“command not found” illegal_commandPossible problem with $PATH or a typo
128Invalid argument to exitexit 3.14159exit takes only integer args in the range 0 – 255 (see first footnote)
128+nFatal error signal “n”kill -9 $PPID of script$? returns 137 (128 + 9)
130Script terminated by Control-C Ctrl-CControl-C is fatal error signal 2, (130 = 128 + 2, see above)
255*Exit status out of rangeexit -1 exit takes only integer args in the range 0 – 255

Example 1: Exit code for “success” and illegal command

$ date
Thu May 13 09:52:04 AEST 2021
$ echo $?
$ dater
-bash: dater: command not found
$ $?
-bash: 127: command not found

Related Posts
No related posts for this content
Apurva Tripathi

Apurva is a PeopleSoft consultant and a big advocate of everything PeopleSoft. He is also a technology enthusiast and loves learning and implementing newer and open source technologies. He spends his spare time updating this blog and likes to read books on self help and productivity.