Fundamentals 4 min read

Understanding Shell Exit Codes: How $? Reveals Command Success or Failure

This article explains how a command’s exit status indicates success or failure in Unix shells, describes the meaning of common return codes, introduces the special $? variable, and provides a comprehensive Bash script example demonstrating various exit scenarios and custom status handling.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Understanding Shell Exit Codes: How $? Reveals Command Success or Failure

In Unix-like shells, every command returns an exit status (also called a return code) that indicates whether the command succeeded (status 0) or failed (any non‑zero status). The exit status can be inspected to control script flow or to report errors.

Common Exit Status Values

0 – Success

Non‑zero – Failure

2 – Incorrect usage of a command

126 – Command found but not executable

127 – Command not found

The Special Variable $?

The built‑in variable $? holds the exit status of the most recently executed command. It works both inside functions and after a whole script finishes, always reflecting the last command’s status.

Script Example Demonstrating Exit Codes

#!/bin/bash

echo -e "Successful execution"
echo -e "====================="
echo "hello world"
# Exit status 0 because the command succeeded
echo "Exit status" $?

echo -e "Incorrect usage"
echo -e "====================="
ls --option   # wrong option, exit status 2
echo "Exit status" $?

echo -e "Command Not found"
echo -e "====================="
bashscript   # command does not exist, exit status 127
echo "Exit status" $?

echo -e "Command is not an executable"
echo -e "============================="
touch execution.sh
ls -l execution.sh
./execution.sh   # not executable, exit status 126
echo "Exit status" $?

echo -e "Custom status"
echo -e "====================="
function test1() {
  if [ ! -x "./execution.sh" ]; then
    echo "\"./execution.sh\" no execute permission!!"
    return 66
  fi
}

test1
echo "Exit status" $?

The script prints messages, deliberately triggers errors, and shows the resulting exit codes after each operation. It also defines a function test1 that checks execution permission and returns a custom status code (66) when the file cannot be run.

Result Overview

Running the script produces a series of outputs, each followed by the corresponding exit status (0, 2, 127, 126, 66). These values illustrate how different failure modes are represented by distinct codes, enabling scripts to react appropriately.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellScriptingBashexit status
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.