Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Exit Status in Shell Scripting

This tutorial provides a detailed explanation of exit statuses in shell scripting, including their importance and how to use them effectively.

1. What is Exit Status?

An exit status is a numerical value returned by a command or script to indicate its success or failure. In shell scripting, the exit status of the last executed command is stored in the special variable $?.

2. Exit Status Conventions

By convention, an exit status of 0 indicates that the command or script executed successfully. A non-zero exit status indicates an error or failure. Different non-zero values can represent different types of errors.

3. Checking Exit Status

You can check the exit status of the last executed command using the $? variable. Here is an example:

#!/bin/bash

echo "Hello, World!"
echo $? # Should print 0

ls nonexistentfile
echo $? # Should print a non-zero value

In this example, the first echo command will succeed, so $? will be 0. The ls command will fail because the file does not exist, so $? will be non-zero.

4. Setting Exit Status in Scripts

You can set the exit status of a script explicitly using the exit command followed by a numerical value. Here is an example:

#!/bin/bash

# Simulate a successful operation
echo "Operation successful"
exit 0

In this example, the script will exit with a status of 0, indicating success.

To indicate an error, you can use a non-zero exit status:

#!/bin/bash

# Simulate an error
echo "Operation failed"
exit 1

In this example, the script will exit with a status of 1, indicating an error.

5. Using Exit Status in Conditional Statements

You can use the exit status in conditional statements to control the flow of your script based on the success or failure of commands. Here is an example:

#!/bin/bash

mkdir mydir
if [ $? -eq 0 ]; then
echo "Directory created successfully"
else
echo "Failed to create directory"
fi

In this example, the script checks the exit status of the mkdir command and prints a message based on whether the command succeeded or failed.

6. Example Script

Here is a complete example script that demonstrates the use of exit statuses:

#!/bin/bash

# Attempt to change to a directory
cd /path/to/directory
if [ $? -ne 0 ]; then
echo "Failed to change directory"
exit 1
fi

# Perform some operation
echo "Performing operation..."
if [ $? -ne 0 ]; then
echo "Operation failed"
exit 1
fi

# If everything succeeded
echo "Script completed successfully"
exit 0

In this example, the script attempts to change to a directory and perform an operation. It checks the exit status after each command and exits with an appropriate status if any command fails.

7. Conclusion

In this tutorial, you learned about exit statuses in shell scripting. Exit statuses are crucial for indicating the success or failure of commands and scripts. By checking and setting exit statuses, you can make your scripts more robust and reliable, ensuring that errors are handled appropriately.