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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

14 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

15 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

19 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

20 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

21 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

22 hours ago