Often when writing Bash scripts, you will need to terminate the script when a certain condition is met or to take action based on the exit code of a command.

In this article, we will cover the Bash exit built-in command and the exit statuses of the executed commands.

Exit Status

Each shell command returns an exit code when it terminates, either successfully or unsuccessfully.

By convention, an exit code of zero indicates that the command completed successfully, and non-zero means that an error was encountered.

The special variable $? returns the exit status of the last executed command:

date &> /dev/null
echo $?

The [date](https://linuxize.com/post/linux-date-command/) command completed successfully, and the exit code is zero:


If you try to run ls on a nonexisting directory the exit code will be non-zero:

ls /nonexisting_dir &> /dev/null
echo $?

The status code can be used to find out why the command failed. Eac

#exit codes #bash #bash exit

Bash Exit Command and Exit Codes
39.90 GEEK