How to exit from Bash script

If you are writing a Bash script or even just executing one, an essential thing you will need to know is how to exit from a Bash script.

There are keyboard combinations that can exit from a Bash script while it is executing in your terminal, and there are ways to exit from within a Bash script using various exit codes. We will show you examples of both.

In this tutorial, you will learn how to exit from a Bash script either from within the script or from the command line while the script is executing on a Linux system.

In this tutorial you will learn:

  • How to exit from a Bash script in terminal
  • How to exit from a Bash script within the script
  • How to use different exit codes within a Bash script
Example of how to make a Bash script exit from within the script
Example of how to make a Bash script exit from within the script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to exit from a Bash script in terminal



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard. A ^C character will appear in your terminal to indicate a keyboard interrupt.

$ ./test.sh
^C

This sends a SIGINT interrupt signal to the script and, 99% of the time, this should immediately exit the script you are running.

The only exception is if a trap has been setup to catch the SIGINT signal. This is the case in scripts that need to finish up a certain task, even if the user is urgent to stop the script prematurely. In this case, you should probably just wait for the script to finish.

NOTE
Read more about Bash traps in our other tutorial on How to modify scripts behavior on signals using bash traps.

Worst case scenario, you can manually kill the script with the kill command. See our other tutorial on How to Kill a Running Process on Linux.

How to exit from a Bash script within the script

Naturally, a Bash script will exit whenever it reaches the end of the script. But sometimes the script is not meant to make it to the end, such as in the case of a conditional statement.

The exit command can be written into a Bash script to manually terminate it at a certain point. An exit code of 0 usually indicates that the script exited without any errors. An exit code of 1 or higher usually indicates that an error was encountered upon exit. However, it is up to the developer to decide what they want these codes to mean in their script.

Let’s look at some examples.

  1. Here is a basic script that will only exit when the first clause of the if statement is true.
    #!/bin/bash
    
    while true; do
    
    echo "enter some text"
    read text
    
    if [[ -n $text ]]; then
            echo "you entered: $text"
            exit 0
    else
            echo "you didn't enter anything!"
    fi
    
    done

    First, we are prompting the user to enter some text. Then, our if statement tests to see if the string contains text or is empty. If it contains text, the script will echo the string entered and then exit the script. If the user does not enter anything, the while loop will continue to execute, and keep prompting them until a string is entered. Here is what it looks like when we execute the script:

    $ ./test.sh 
    enter some text
    hello
    you entered: hello
    

    Now we can execute the following command to see what exit code our script exited with.

    $ echo $?
    0
    

    As intended, we have an exit code of 0. Note that we could also just use exit in our script instead of exit 0. Both will exit with a code of 0.

  2. Now that you have an idea how exit codes work, let’s look at a more practical example. The following script will exit with a code of 1 if the user is logged in as root when they execute the script. If they are logged in as a normal user, the script will proceed with its functions and then exit with a code of 0.
    #!/bin/bash
    
    user=$(whoami)
    
    if [ $user = root ]; then
            echo "Don't execute the script as root"
            exit 1
    fi
    
    # do some stuff
    echo "All done..."
    exit 0

    Let’s see what happens when we execute the script with or without root privileges.

    $ ./test.sh 
    All done...
    $ echo $?
    0 
    
    $ sudo ./test.sh 
    Don't execute the script as root
    $ echo $?
    1
    


Closing Thoughts

In this tutorial, you learned how to exit from a Bash script on a Linux systmem. This included exiting from the script while it is executing in terminal, and how to exit from within a Bash script that you are writing. You also saw how to use exit codes, which allow us to indicate whether the script exited successfully or from an error, etc.



Comments and Discussions
Linux Forum