LinuxII · Shell and Command-Line OperationsShell fundamentals
Shell anatomy — commands, options, arguments, exit codes
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
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:
| Part | Example | Purpose |
|---|---|---|
| Command | ls | The program to run. Resolved via PATH (or a builtin). |
| Short option | -l | Single-letter flag. Usually a single hyphen. |
| Long option | --long | Full-word flag. GNU-style double hyphen. |
| Option argument | --target=eth0 | Some options take a value, attached with = or space. |
| Positional argument | /var/log | Required input to the command. |
| Compound command | `cmd1 | cmd2` |
$ type ls; type cd; type sudols is /usr/bin/ls
cd is a shell builtin
sudo is /usr/bin/sudoExit 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.1to125— failure. The exact value is command-specific but follows conventions:1is generic failure,2is misuse (often bad arguments),126is “command found but not executable”,127is “command not found”.128+N— the command was killed by signalN.130means Ctrl-C (SIGINT, signal 2);137means SIGKILL (signal 9);143means SIGTERM (signal 15).
$ true; echo $?; false; echo $?0
1Production scripts depend on exit codes. The conditional
constructs &&, ||, and if all read $?.
$ grep -q 'root' /etc/passwd && echo found || echo missingfoundThree output channels
A command can produce output on three separate channels:
| Channel | Default destination | Default behaviour | Convention |
|---|---|---|---|
| stdout (file descriptor 1) | terminal | Display normally | Program’s normal output |
| stderr (file descriptor 2) | terminal | Display normally | Diagnostics, warnings, errors |
| exit code | shell variable $? | Invisible | Success / failure as an integer |
A robust script redirects stderr separately from stdout because pipelines should not mix normal output with errors:
$ find /etc -name '*.conf' 2>/dev/null | wc -l418Illustrative output
Command resolution
When you type ls, the shell has to find a program to run. The order:
- Shell functions (defined with
function name { … }). - Builtins (
cd,echo,type,read,printf,test,[,[[). - Lookups in
$PATH, left to right.
Knowledge check
Knowledge check · 3 questions
Q1. A process was killed by SIGTERM (signal 15) by the kernel. What exit code does the shell report?
Q2. `cd` has to be a shell builtin, because a child process cannot change the working directory of the shell that started it.
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.