Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsShell fundamentals

Shell anatomy — commands, options, arguments, exit codes

Foundation⏱ ~8 minbashechotypetruefalse

What you'll learn

  • Identify the parts of a shell command line
  • Distinguish short and long options, and options that take arguments
  • Read and use shell exit codes
  • Distinguish stdout, stderr, and exit status as three channels of output

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Every shell command line follows the same grammar: a command, options, arguments. Producing results is only half the job; reporting whether those results succeeded — via exit codes and stderr — is the other half. Production scripts are built on both halves.

Anatomy of a command line

$ command -o --long-option=value positional-arg another-arg

The parts:

PartExamplePurpose
CommandlsThe program to run. Resolved via PATH (or a builtin).
Short option-lSingle-letter flag. Usually a single hyphen.
Long option--longFull-word flag. GNU-style double hyphen.
Option argument--target=eth0Some options take a value, attached with = or space.
Positional argument/var/logRequired input to the command.
Compound command`cmd1cmd2`
Read-only / Safehow is this resolved?
$ type ls; type cd; type sudo
ls is /usr/bin/ls
cd is a shell builtin
sudo is /usr/bin/sudo

Exit codes

After every command finishes, the shell records an exit code — a small integer. The convention is universal:

  • 0 — success. The command did what you asked.
  • 1 to 125 — failure. The exact value is command-specific but follows conventions: 1 is generic failure, 2 is misuse (often bad arguments), 126 is “command found but not executable”, 127 is “command not found”.
  • 128+N — the command was killed by signal N. 130 means Ctrl-C (SIGINT, signal 2); 137 means SIGKILL (signal 9); 143 means SIGTERM (signal 15).
Read-only / Safeexit codes
$ true; echo $?; false; echo $?
0
1

Production scripts depend on exit codes. The conditional constructs &&, ||, and if all read $?.

Read-only / Safeconditional chain
$ grep -q 'root' /etc/passwd && echo found || echo missing
found

Three output channels

A command can produce output on three separate channels:

ChannelDefault destinationDefault behaviourConvention
stdout (file descriptor 1)terminalDisplay normallyProgram’s normal output
stderr (file descriptor 2)terminalDisplay normallyDiagnostics, warnings, errors
exit codeshell variable $?InvisibleSuccess / failure as an integer

A robust script redirects stderr separately from stdout because pipelines should not mix normal output with errors:

Read-only / Safesilence permission errors
$ find /etc -name '*.conf' 2>/dev/null | wc -l
418

Illustrative output

Command resolution

When you type ls, the shell has to find a program to run. The order:

  1. Shell functions (defined with function name { … }).
  2. Builtins (cd, echo, type, read, printf, test, [, [[).
  3. Lookups in $PATH, left to right.

Knowledge check

Knowledge check · 3 questions

  1. Q1. A process was killed by SIGTERM (signal 15) by the kernel. What exit code does the shell report?

  2. Q2. `cd` has to be a shell builtin, because a child process cannot change the working directory of the shell that started it.

  3. Q3. Which of the following commands would typically return exit code 127? Select all that apply.

Passing score: 75%. Answers are checked in this browser.