How to Check the Exit Status of a Command in a Bash Script

By Birtchum Thompson | March 11, 2020

This tutorial demonstrates how to check the exit status of a Bash command in a script.

When writing a Bash script, it is often useful to capture the exit code of the previous command and use it to determine the next command. This is easily accomplished with Bash using the following code:


In this command, $? is a special variable that holds the exit code previous command and -eq is the numeric comparison operator in Bash. If a command completes successfully, the exit code will be zero. If the exit code is not zero, an error has most-likely occurred.

Share this Content

Join the Discussion

Also Read

UFW, Ubuntu, and firewall logos on dark grey background
Allow SMB in UFW

Allow SMB traffic in UFW on Ubuntu & Debian.

Python logo and red trash can
Delete Files Older than X Days in Python

Create a Python script to delete files older than x days.

Blue KDE Logo
Configure Screen Locking Shortcut in KDE

Learn to configure the screen locking shortcut in KDE.

Clock and calendar image with crontab -e as text
Open Crontab with Nano

Learn how to use nano to edit crontab files.