How To

Bash Exit Command And Exit Codes Explained

Introduction

The Bash exit command is used to stop a script and return an exit code to the Linux shell. Exit codes are important because they tell whether a command or script completed successfully or failed. If you are learning Bash scripting, understanding exit codes will help you write better scripts for automation, error handling, system checks, and cybersecurity tasks.

In Linux, every command returns an exit status. A successful command usually returns 0, while an error returns a non-zero value. Bash scripts can use these exit codes to decide what to do next.

What Is The Exit Command In Bash?

The exit command stops the execution of a Bash script.

Basic syntax:

exit

You can also provide an exit code:

exit 0

or:

exit 1

Here, exit 0 usually means success, and exit 1 usually means failure.

Basic Bash Exit Command Example

Create a new Bash script:

nano exit-example.sh

Add the following code:

#!/bin/bashecho "Script started"echo "Script completed successfully"exit 0

Save and run the script:

chmod +x exit-example.sh./exit-example.sh

Output:

Script startedScript completed successfully

The script ends with exit 0, which means it completed successfully.

How To Check Exit Codes In Bash

Bash stores the exit code of the last command in the special variable $?.

Example:

ls /etc/passwdecho $?

Output:

0

Now try a command that fails:

ls /wrong-directoryecho $?

Output:

2

The non-zero exit code means the command failed.

Using Exit Codes In Bash Scripts

Exit codes are useful when checking whether a command was successful.

#!/bin/bashfile="/etc/passwd"if [[ -f "$file" ]]; then    echo "File exists"    exit 0else    echo "File does not exist"    exit 1fi

Run the script:

chmod +x check-file.sh./check-file.sh

If the file exists, the script exits with code 0. If the file does not exist, it exits with code 1.

Cybersecurity Example: Check If A Tool Is Installed

Exit codes are useful in cybersecurity scripts when checking tools.

#!/bin/bashtool="nmap"if command -v "$tool" > /dev/null 2>&1; then    echo "$tool is installed"    exit 0else    echo "$tool is not installed"    exit 1fi

This script checks whether nmap is installed. If installed, it returns success. If missing, it returns failure.

Common Exit Code Meanings

Exit CodeMeaning
0Success
1General error
2Misuse of shell command
126Command found but not executable
127Command not found
130Script stopped with CTRL + C

Conclusion

The Bash exit command and exit codes are important for writing reliable scripts. They help your script clearly report success or failure. You can use exit 0 for success and non-zero exit codes for errors.

For beginners, learning exit codes is useful for error handling, automation, Linux administration, and cybersecurity scripting. Once you understand exit codes, you can create smarter Bash scripts that react properly when commands fail or succeed.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

1 day ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

1 day ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

1 day ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

1 day ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

1 day ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago