Welcome! In this lesson, we will explore the concept of exit statuses in shell scripts. An exit status indicates whether a command was successful or if an error occurred. This lesson will guide you through the basics of exit statuses, including how to check them, handle errors, and use them to control the flow of your scripts.
Let's get started!
In the Unix-like operating system, every command returns an exit status when it finishes executing. This exit status is a number where:
0
indicates success.- Any non-zero value indicates an error (the specific number can provide information about the kind of error).
To check the exit status of a command, we use the special variable $?
.
Suppose we have a directory called projects
. Inside projects
, there is a file p1.txt
. We can observe the exit status for ls
commands as follows:
Here's an example:
Bash1#!/bin/bash 2 3echo "Running a successful command (ls)..." 4ls projects 5echo "Exit status: $?" 6 7# Demonstrating a failing command 8echo "Running a failing command..." 9ls nonexistent_directory # Causes error 10echo "Exit status: $?"
- The
ls projects
command lists the contents of theprojects
directory, which is justp1.txt
. Since it is a valid directory, the exit status is0
. - The
ls nonexistent_directory
command attempts to list a directory that does not exist, resulting in an error. The exit status is a non-zero value, typically2
.
The output of the code is:
Plain text1Running a successful command (ls)... 2p1.txt 3Exit status: 0 4Running a failing command... 5Exit status: 2
The following error message is also output:
Plain text1ls: cannot access 'nonexistent_directory': No such file or directory
We can see that the successful ls
command has an exit status of 0
(success). The unsuccessful ls
command has an exit status of 2
and outputs a descriptive error message.
The exit
command in a shell script is used to terminate the script and optionally provide an exit status. The exit status is a numeric value that indicates the result or outcome of the script's execution. By convention, an exit status of 0 indicates that the script completed successfully without any errors.
Any non-zero exit status indicates that the script encountered an error or abnormal condition. Common non-zero exit statuses include:
- 1: General errors.
- 2: Misuse of shell builtins (according to the Bash documentation).
- 126: Command invoked cannot execute.
- 127: Command not found.
- 128: Invalid argument to exit.
- 130: Script terminated by Control-C.
When used in a script, you can specify the exit status as an argument to the exit
command, like exit 1
, to indicate a specific type of error. If no argument is provided to exit
, the script will terminate with the status of the last executed command.
Sometimes, you might want to exit your script with a specific status. You can use the exit
command followed by a status code. For example:
Bash1#!/bin/bash 2echo "Exiting with a custom status (42)..." 3exit 42
exit 42
terminates the script and returns status 42
to the calling shell or process. This calling shell or process can access this exit status using $?
.
You can use exit statuses in conditional statements to control the flow of your script. Let's see an example of checking command success using an if
statement. In this example, we want to create a directory called projects
, but it already exists.
Bash1#!/bin/bash 2 3# Attempting to create a directory 4echo "Attempting to create a directory..." 5mkdir projects 6status=$? 7 8if [ $status -eq 0 ]; then 9 echo "Directory created successfully." 10 exit 0 11else 12 echo "Failed to create directory." 13 echo "Exit status: $status" 14 exit $status 15fi
In this code:
mkdir projects
attempts to create a directory namedprojects
.status=$?
captures the exit status of themkdir
command and stores it in thestatus
variable.if [ $status -eq 0 ]
checks if the captured exit status is0
, which indicates success.- If
$status
does equal0
:echo "Directory created successfully."
prints the success messageexit 0
exits the script with a success status (0
). The calling shell or process can access this exit status using$?
.
- The
else
clause executes if$status
does not equal 0.echo "Failed to create directory."
prints a failure message.echo "Exit status: $status"
prints$status
which in this case is1
exit $status
exits the script with a failure status of1
The output of this script is:
Plain text1Attempting to create a directory... 2Failed to create directory. 3Exit status: 1
The following error is also output:
Plain text1mkdir: cannot create directory ‘projects’: File exists
Using conditional statements, we are able to provide informative output when running our script.
Sometimes, you want your script to exit when it first encounters an error. The set -e
command in your script will cause the script to exit immediately if any command returns a non-zero status. This can be useful for preventing your script from continuing in an erroneous state. Let's take a look:
Bash1#!/bin/bash 2 3set -e 4 5echo "Running a command that will fail..." 6ls nonexistent_directory 7echo "This line will not be executed if the ls command fails." 8cd nonexistent_directory
set -e
ensures that the script exits immediately if any command fails- The
ls
command attempts to list the contents of a nonexistent directory. - The script exits when the
ls
command fails - The
cd
command does not execute because the script already exited
The output of this script is:
Plain text1Running a command that will fail...
The error that is output is:
Plain text1ls: cannot access 'nonexistent_directory': No such file or directory
By using the set -e
command, we can exit the script, preventing the script from trying to switch into a nonexistent directory with the cd
command
Congratulations on completing this lesson on exit statuses! In this lesson, you learned how to:
- Check the exit status of a command using
$?
. - Use the
exit
command to terminate a script with a specific status. - Handle exit statuses using conditional statements to control script flow.
- Utilize
set -e
to make your script exit immediately on errors.
Understanding exit statuses is a fundamental skill for writing robust shell scripts that handle errors gracefully. In the next practice section, you will have the opportunity to apply what you've learned and reinforce your knowledge by writing scripts that use exit statuses effectively.
Get ready to dive into the practice section and start scripting with confidence. Happy scripting!