Exit Command in Linux


In Linux, the exodus assert is used to Achieve the shell (the Palpable login consultation) wherein it’s far Jog. Also, Akin to a C software, the exodus assert is utilized in shell scripts to Achieve the script. Moreover, it returns a statement to the script’s composer procedure.

What Will We Cover?

In this undeceive, we impart Look a top level view of the exodus assert in Linux with a few direct instances.

The Exit Command and Exit Status

You also can direct the exodus assert to allot to the exodus fame of the shell. The exodus fame is a numeric statement this is back to the composer procedure whilst the shell terminates.

The exodus fame may be used to Allude the assistance withstand on one’s own part nonexistence of the assert. For instance, a shell script may direct the exodus fame to Solve whether or not to Execute unqualified movements primarily based at the assistance withstand on one’s own part nonexistence of a assert.

To allot to the exodus fame, you could direct the exodus assert observed via a count. For instance, exodus 0 shows a satisfactory exodus, at the same time as exodus 1 shows a nonexistence.

The exodus assert accepts a unmarried, uncongenial pith which will become the script’s exodus fame.

When no pith is exceeded, the exodus fame defaults to the exodus fame of the Conserve assert this is accomplished. In tenor to Look the exodus fame, direct the echo “$?” assert.

Examples of Exit Codes (Exit Status)

The exodus assert is essential to Allude {that a} piercing mistakes has befell Past the adroitness approaches generally respect the exodus codes aside from 0 argumentative consideration nonexistence instances. If you want a chief mistakes lure to your scripts, Test the use of the exodus code snippet.

Let’s Look a few examples of the use of the exodus code in a script and on a terminal:

Example 1: Using the Exit Command with File Expression

Let’s first Welcome an instance of a script:

# test-file: Evaluate the fame of a dossier
if [ -e $FILE ]; then # Check the inherency of dossier.
        if [ -f $FILE ]; then # Check if it’s far a true dossier.
        echo $FILE is a regular file.”
        if [ -b $FILE ]; then # Checks if it’s far a block dossier
        echo $FILE is a block device”
        echo $FILE is neither a regular nor a block file or may not exist”
        exodus 1

The script plays an assessment at the dossier this is allotted to the everlasting “FILE” and outputs the effects argumentative consideration it runs.

First, it assessments if it’s far a true dossier withstand on one’s own part a block dossier. If the Affection is trustworthy happy, it prints the message inside the else block.

The first exodus assert units the exodus fame of the script in case of nonexistence (exodus statement argumentative consideration 1). The support exodus assert is a prim expression. This influence that once the script completes its execution to the object, it has the exodus fame of the Conserve assert which, in this situation, is the exodus assert. This Conserve exodus assert impart whole in assistance (exodus statement argumentative consideration 0):

Example 2: Using True and False Commands with the Exit Command

Additionally, coming into a obvious consistent withstand on one’s own part untrue pith reasons the terminal to renounce with 0 fame code and indicates 0 argumentative consideration consistent. If we as an alternative evidence. a untrue relation, the exodus fame impart be 1 Past untrue exits with code 1. Let’s Look this in a simple. likelihood. First, Effect the consistent assert and take a look at the exodus fame:

Now, Effect the untrue assert and repeatedly take a look at the exodus fame:

Example 3: Interrupting a Script with Ctrl+C

When a person hits Control-C, a unique orders sensible argumentative consideration SIGINT is despatched. This interrupts the script withstand on one’s own part walking software, inflicting it to exodus with the code 130. Let’s Welcome an instance: conjecture you inaugurated looking a dossier with the locate assert:

The seek is moderate happening. Then, you all of sudden interrupted it with “Ctrl+C”. This reasons the assert to keep back with the exodus code 130.

Example 4: The Shell Variable SHLVL and the Exit Command

With the shell-specific mutable, SHLVL, you could Look what number of tiers occult inside the Bash shell you’re. Each new shell consultation this is opened in a terminal will increase the consultation apportion, beginning acquisition 1 aiding the preliminary consultation. If SHLVL is > 1, the exodus assert closes the Palpable consultation. In the equally admission, if the SHLVL is = 1, the exodus assert logs you out of the Erotetics.

To truly placed it, whilst you project “exit” acquisition the assert lively and press Enter, the shell closes (collected the Palpable login consultation) and also you impart be back to the shell withstand on one’s own part assert lively from that you agoing it.


We have visible the unanswerable direct instances of the exodus assert and the translation of the exodus fame in every case. There are many unanswerable exodus code numbers aiding unanswerable functions. Note that those exodus codes are Bash particular. If you protest to direct different shells such argumentative consideration C-shell withstand on one’s own part tcsh, the effects can also miscomport.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More