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.
Join the Discussion
Also Read
Create a Python script to delete files older than x days.
Read Now!