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 in terminal
- How to exit from a Bash script within the script
- How to use different exit codes within a Bash script
|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
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to exit from a Bash script in terminal
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 + Ccombination on your keyboard. A
^Ccharacter 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.
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.
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.
- Here is a basic script that will only
exitwhen the first clause of the
ifstatement 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
ifstatement tests to see if the string contains text or is empty. If it contains text, the script will
echothe string entered and then
exitthe script. If the user does not enter anything, the
whileloop 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
exitin our script instead of
exit 0. Both will exit with a code of
- 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
1if 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
#!/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
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.