Get Exit Code of Last Bash Command on Linux

Get Exit Code of Last Bash Command on Linux

On Linux, the exit code of a command or script is a numerical value that indicates whether the command or script executed successfully or encountered an error. A successful execution typically yields an exit code of 0, while non-zero values indicate various types of errors. This numerical code acts as a communication mechanism between scripts, programs, and the operating system. This tutorial shows get exit code of last Bash command on Linux.

The $? is a special variable in Bash that holds the exit code of the most recently executed command. After a command is executed, its exit code is set, and we can access it using $?.

As an example, execute the ls command in the terminal. To retrieve the exit code of the most recent command, run the following command:

echo $?

It returns 0, indicating successful execution of the preceding command.

Leave a Comment

Cancel reply

Your email address will not be published.