Agentic Research

Exit code

Also: 退出碼 · 結束代碼 · exit status · $?

The integer a program hands back as it exits: 0 means success, anything else means failure — how a program reports its outcome to the world.

When you will meet it

Scripts, CI systems and AI agents do not read output text to decide whether a step worked — they check one integer: the exit code. An agent's "if that failed, try something else" loop hinges on it. Without this concept you cannot see why an agent retries after a failure, or why it barrels on after a command that "looked wrong" on screen — because it saw 0.

An analogy

Like the signature box on a delivery slip: the recipient does not read you a report, they just sign (0) or refuse (non-zero). What was in the box (stdout) and whether the delivery succeeded are two separate things.

Minimal example

true;  echo $?                        # 0:成功
false; echo $?                        # 1:失敗
ls /not-exist 2>/dev/null; echo $?    # 非 0:失敗(數字是程式自己定義的)

false || echo "上一個失敗了(非 0),所以我執行了"
true  && echo "上一個成功了(0),所以我執行了"

$? is the previous command's exit code (bash/zsh; PowerShell uses $LASTEXITCODE). Non-zero meanings are up to each program, but shells share conventions: 127 = command not found, 130 = interrupted with Ctrl+C. || runs the next thing only on failure, && only on success — automation scripts are built from exactly these two operators.

What people get wrong

  • Judging success by whether the output looks healthy. Programs print plausible logs and still exit non-zero — and scroll walls of warnings while exiting 0. Scripts, CI and agents trust the number, not the performance.
  • Checking only the last command's code in a chain. Intermediate failures get swallowed by whatever runs after; that is why scripts use set -e (bash) to stop at the first failure, and a well-built agent checks the number after every single step.

Related terms

Next