An exit code or exit standing tells us concerning the standing of the final executed command. Whether or not the command was accomplished efficiently or ended with an error. That is obtained after the command terminates.
The fundamental ideology is that packages return the exit code 0
to point that it executed efficiently with out points. Code 1
or something apart from 0 is taken into account unsuccessful.
There are various extra exit codes apart from 0 and 1, which I am going to cowl on this article.
Varied exit codes in Linux shell
Allow us to take a fast have a look at the distinguished exit codes within the Linux shell:
Exit code | That means of the code |
---|---|
0 |
Command executed with no errors |
1 |
Code for generic errors |
2 |
Incorrect command (or argument) utilization |
126 |
Permission denied (or) unable to execute |
127 |
Command not discovered, or PATH error |
128+n |
Command terminated externally by passing alerts, or it encountered a deadly error |
130 |
Termination by Ctrl+C or SIGINT (termination code 2 or keyboard interrupt) |
143 |
Termination by SIGTERM (default termination) |
255/* |
Exit code exceeded the vary 0-255, therefore wrapped up |
📋
The termination alerts like 130
(SIGINT or ^C
) and 143
(SIGTERM) are distinguished, that are simply 128+n
alerts with n
standing for the termination code.
Now that you’re briefly aware of the exit codes let’s have a look at about their utilization.
Retrieving the exit code
The exit code of the beforehand executed command is saved within the particular variable $?
. You possibly can retrieve the exit standing by operating:
echo $?
This will probably be utilized in all our demonstrations to retrieve the exit code.
Notice that the exit command helps carrying the identical exit code of the earlier command executed.
Exit code 0
Exit code 0
signifies that the command is executed with out errors. That is ideally the perfect case for the completion of instructions.
For instance, allow us to run a fundamental command like this
neofetch
echo $?

This exit code 0
signifies that the actual command was executed efficiently, nothing roughly. Allow us to display some extra examples.
It’s possible you’ll strive killing a course of; it’ll additionally return the code 0
.
pkill lxappearance

Viewing a file’s contents will even return an exit code 0, which means solely that the ‘cat’ command executed efficiently.
Exit code 1
Exit code 1
can be a standard one. It usually means the command terminated with a generic error.
For instance, utilizing the package deal supervisor with out sudo permissions ends in code 1. In Arch Linux, if I do this:
pacman -Sy
It would give me exist code as 1 that means the final command resulted in error.

📋
If you happen to do this in Ubuntu-based distros (apt replace
with out sudo), you get 100 as an error code for operating ‘apt’ with out permissions. This isn’t a standardized error code, however one particular to apt.
Whereas it is a normal understanding, we are able to additionally interpret this as “operation impermissible”.
Operations like dividing by zero additionally end in code 1.

Exit code 2
This exit code is given out when the command executed has a syntax error. Misusing the arguments of instructions additionally outcomes on this error.
It usually means that the command couldn’t execute because of incorrect utilization.
For instance, I added two hyphens to an possibility that is presupposed to have one hyphen. Code 2 was given out.
grep --z file.txt

When permission is denied, like accessing the /root folder, you get error code 2.

Exit code 126
126 is a peculiar exit code since it’s used to point a command or script was not executed because of a permission error.
This error may be discovered whenever you strive executing a shell script with out giving execution permissions.

Notice that this exit code seems just for the ‘execution‘ of scripts/instructions with out enough permissions, which is completely different from a generic Permission Denied error.
So, on’t confuse it with the earlier instance you noticed with exit code 2. There, ls command ran and the permission challenge got here with the listing it was making an attempt to execute. Right here, the permission points got here from the script itself.
Exit code 127
That is one other frequent one. Exit code 127 refers to “command not discovered“. It normally happens when there is a typo within the command executed or the required executable is just not within the $PATH variable.
For instance, I usually see this error after I strive executing a script with out its path.

Or when the executable file you are making an attempt to run, is just not listed within the $PATH
variable. You possibly can rectify this by including the guardian listing to the PATH variable.
The best way to Add a Listing to PATH in Linux
Be taught all of the important steps about including a listing to the PATH in Linux and making these modifications completely.

You will additionally get this exit code whenever you sort instructions that don’t exist.

Exit code collection 128+n
When an software or command is terminated or its execution fails because of a deadly error, the adjoining code to 128 is produced (128+n), the place n is the sign quantity.
This contains all kinds of termination codes, like SIGTERM
, SIGKILL
, and so on that apply to the worth ‘n’ right here.
Code 130 or SIGINT
SIGINT
or Signal for Keyboard Interrupt is induced by interrupting the method by termination sign 2, or by Ctrl+C.
Because the termination sign is 2, we get a code 130 (128+2). This is a video demonstrating the interrupt sign for lxappearance
.
SIGINT(2) termination or Keyboard Interrupt (^C) that offers code 130
Code 137 or SIGKILL
The SIGKILL
termination signal that kills the method immediately has a termination sign 9. That is the final technique one ought to use whereas terminating an software.
The exit code thrown is 137 for the reason that termination sign is 9 (128+9).
SIGKILL(9) termination that offers code 137
Code 143 or SIGTERM
SIGTERM
or Signal to Time periodinate is the default habits when a course of is killed with out specifying arguments.
The termination code for SIGTERM is 15, therefore this sign will get an exit code of 143 (128+15).
SIGTERM(15) termination that offers code 143
There are different termination alerts that you could be not have recognized earlier than; they too have their very own exit codes just like these. You possibly can test them out right here:
The best way to use SIGINT and different Termination Indicators in Linux
Terminating executing course of is extra than simply kill -9. Listed here are a number of the distinguished termination alerts and their utilization.

📋
Notice that these alerts might not seem if terminated from the identical session from which the method was began. If you happen to’re reproducing these, terminate from a distinct shell.
On a private be aware, sign 128 was unimaginable to breed.
What if the code exceeds 255?
Current variations of Bash retain the unique exit code worth even past 255, however usually, if the code exceeds 255, then it’s wrapped up.
That’s, code 256 turns into ‘0’, 257 turns into ‘1’, 383 turns into ‘127’, and so forth and so forth. To make sure higher compatibility, hold the exit codes between 0 and 255.
Wrapping up
I hope you discovered one thing concerning the exit codes within the Linux shell. Utilizing them can turn out to be useful for troubleshooting numerous points.
In case you are utilizing these codes in a shell script, ensure you perceive the that means of every code in an effort to make it simpler for troubleshooting.
In case you want a reference, take a look at the Bash collection right here:
Bash Fundamentals #1: Create and Run Your First Bash Shell Script
Begin studying bash scripting with this new collection. Create and run your first bash shell script within the first chapter.

That is all concerning the article. Be at liberty to let me know within the feedback part if I’ve missed something.