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.
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.
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.
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.
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.
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.
| Exit Code | Meaning |
|---|---|
0 | Success |
1 | General error |
2 | Misuse of shell command |
126 | Command found but not executable |
127 | Command not found |
130 | Script stopped with CTRL + C |
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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…