All courses

Command output and exit codes

Tell normal output from error output. Explain that a command reports success (exit code 0) or failure (non-zero), which is how an agent decides whether a step 'worked'.

Builds on: Terminal basics: where you are and running things

Read it card by card Start the whole course

How does anyone know a command worked?

You ask the agent to run your script `convert.py`. Text scrolls by, then the agent says: “The script ran successfully.” How could it know? It can't judge from a feeling about the text. Every command returns more than the words you see on screen, and that extra signal is what the agent, and you, should rely on.

Two text channels and one number

A running program has two output channels. **Standard output** (stdout) carries normal results, like “Wrote 120 rows”. **Standard error** (stderr) carries complaints: errors and warnings. In a terminal both print to the same screen, so they look alike. When the program ends, it also returns a number, the **exit code**. `0` means “I succeeded”. Anything else means “I didn't”. The agent receives the text and that number.

The rules for reading a result

- `0` = success. Any other number (on Linux and macOS, `1` to `255`) = not success. - What each non-zero number means is up to the program. For example, `grep` returns `1` when it simply finds no match, and `2` for a real error. - To see the code, run `echo $?` right after a command in bash or zsh. In PowerShell, use `$LASTEXITCODE`. - To split the channels, run `<command> 2> err.txt`. Stderr goes into the file and stdout stays on screen. - The words can mislead. “Done!” with code `1` is a failure. Warnings with code `0` are a success.

Seeing both channels and the exit code

In the first run, the normal result appears on screen and the exit code is `0`. In the second run, nothing appears on screen, because the complaint went to stderr and `2>` sent it into `err.txt`. The exit code is `1`. `cat` prints the file, so you can read the error.
$ python convert.py data.csv 2> err.txt
Wrote 120 rows to out.csv
$ echo $?
0
$ python convert.py nope.csv 2> err.txt
$ echo $?
1
$ cat err.txt
error: nope.csv not found

When the exit code belongs to someone else

The agent shows: ⚙ Run: python convert.py big.csv | tail -n 3 The `|` (pipe) feeds the script's stdout into `tail`, which keeps only the last 3 lines. The script crashes halfway. By default in bash and zsh, the reported exit code is that of the last command, `tail`. That code is `0`, so the agent says “succeeded”. The error line still appears, because the pipe doesn't carry stderr. Read that error line yourself, then type: ▸ You: Rerun without `| tail` and tell me the exit code.

Remember

Normal results go to stdout and complaints go to stderr, but both look the same on screen. The exit code decides: `0` means success, anything else means failure. That number, not the wording, is how a step is judged to have “worked”. With a pipe, the code you see belongs to the last command, not your script.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.