Linux and Unix command-line workflows

Follow one report command line from the shell's rewriting of its words, through pipes, redirections and exit statuses, to the signals that stop a process. OS practice and environments · Operating systems.

You type one line in /srv/atlas and press Enter: grep 'disk full' incoming/*.log | sort > out/report.txt. You typed a star, two quotes, a pipe and an arrow, yet grep never receives any of them. Which words does it get, who opens out/report.txt, and what does the shell report in $? if one of the logs cannot be read?

The shell rewrites the line, then launches

The shell is an ordinary program that reads your line, rewrites it, and only then starts the commands. First it splits the line into words. Quotes keep a word whole, so 'disk full' stays one word even though it holds a space. Then it expands any unquoted pattern containing *: the pattern becomes the sorted names that match, each as its own word. In Bash and other POSIX shells, a pattern that matches no name is by default left as typed, so the program receives a literal star (zsh reports an error instead).

Take two files, incoming/app.log with 3 lines and incoming/db.log with 2. The shell turns incoming/*.log into incoming/app.log and incoming/db.log. It then forks one process for grep and one for sort, joined by a pipe, and each child runs its program with exec. The grep process starts with the argument list grep, disk full, incoming/app.log, incoming/db.log. It reads 5 lines, finds 3 that contain disk full, and writes them into the pipe. sort reads those 3 lines and writes them, in order, to out/report.txt.

Quotes, a wildcard and a pipe into a file

Read the steps as text

  1. The shell reads the line "grep 'disk full' incoming/*.log | sort > out/report.txt".
  2. The quotes keep 'disk full' together as one word for the grep process. The shell expands incoming/*.log into incoming/app.log and incoming/db.log.
  3. The shell creates a pipe from the grep process to the sort process.
  4. The shell forks two processes, which run at the same time; the next steps take them in turn. The grep process gets [grep] [disk full] [incoming/app.log] [incoming/db.log] and the sort process gets [sort].
  5. The shell points the standard output of the sort process at out/report.txt, creating it.
  6. The grep process reads 5 lines from its files and writes 3 lines to the pipe, then exits with status 0 because a line matched.
  7. The sort process reads 3 lines from the pipe and writes 3 lines to out/report.txt, then exits with status 0.
  8. The shell sets $? to 0, the status of the last command, the sort process.

Drop the quotes and the same words are split differently: grep disk full incoming/app.log gives grep the pattern disk and two file names, full and incoming/app.log. There is no file called full, so grep reports an error and exits with status 2.

Redirections run left to right

A redirection is applied by the shell, in the order written, before the program starts. > out/errors.txt opens that file as standard output, creating it if needed and emptying it unless you write >>. 2>&1 makes standard error, descriptor 2, a copy of wherever standard output points at that moment. The order therefore matters.

Take grep ERROR app.log gone.log, where gone.log does not exist. Written > out/errors.txt 2>&1, output goes to the file first and error output is copied to it, so the file holds the matching lines and the error message. Written 2>&1 > out/fails.txt, error output is copied to the terminal first and only then does output move to the file, so the message stays on the screen and the file holds only the matches.

Redirect the file first, then copy the error stream

Read the steps as text

  1. The shell reads the line "grep ERROR app.log gone.log > out/errors.txt 2>&1".
  2. The shell forks a process whose argument list is [grep] [ERROR] [app.log] [gone.log].
  3. The shell points the standard output of the grep process at out/errors.txt, creating it. Standard error of the grep process is then pointed at out/errors.txt too, copying where standard output points.
  4. The grep process reads 3 lines from its files and writes 2 lines and 1 error line to out/errors.txt, then exits with status 2 because it could not read a file.
  5. The shell sets $? to 2, the status of the last command, the grep process.

Copy the error stream first, then redirect the file

Read the steps as text

  1. The shell reads the line "grep ERROR svc.log lost.log 2>&1 > out/fails.txt".
  2. The shell forks a process whose argument list is [grep] [ERROR] [svc.log] [lost.log].
  3. Standard error of the grep process is pointed at where standard output points now, the terminal. The shell points the standard output of the grep process at out/fails.txt, creating it.
  4. The grep process reads 3 lines from its files, writes 2 lines to out/fails.txt and writes 1 error line to the terminal, then exits with status 2 because it could not read a file.
  5. The shell sets $? to 2, the status of the last command, the grep process.

The target is opened under the ordinary path and permission rules. Suppose the directory out lacks search permission for you. The forked process tries to open out/report.txt, is refused, and the shell prints a permission error and never runs the program grep. Bash reports status 1 (another shell may use a different non-zero value). Search permission on a directory is the access rule applied to every path component, which path resolution walks one name at a time. Giving yourself search permission on the directory, for example with chmod u+x out when you own it, lets the same line work; changing the program or its input would not.

A target the shell cannot open

Read the steps as text

  1. The shell reads the line "grep ERROR incoming/*.log > out/report.txt".
  2. The shell expands incoming/*.log into incoming/app.log and incoming/db.log.
  3. The shell forks a process whose argument list is [grep] [ERROR] [incoming/app.log] [incoming/db.log].
  4. The grep process tries to open out/report.txt for writing, but out has no search permission, so the open fails, "bash: out/report.txt: Permission denied" reaches the terminal and grep is never executed.
  5. The shell reports status 1 for the grep process, which never started.
  6. The shell sets $? to 1, the status of the last command, the grep process.

Pipelines and exit status

Every | creates a pipe, and the commands run at the same time. A writer waits when the pipe is full, and receives the SIGPIPE signal if the reader has already exited; the pipes lesson covers both. When a process ends it returns a status from 0 to 255. By convention 0 means success. grep uses three values: 0 when a line matched, 1 when none did, and 2 when an error such as an unreadable file occurred.

The shell sets $? to the status of the last command in the pipeline. In grep ERROR logs/*.log | sort, suppose logs/cache.log is unreadable. grep still writes the matches from the other two files into the pipe, writes one error line to the terminal and exits with 2. sort then exits with 0, so $? is 0 and a script that checks only $? believes the whole line worked.

A failure the pipeline status hides

Read the steps as text

  1. The shell reads the line "grep ERROR logs/*.log | sort".
  2. The shell expands logs/*.log into logs/api.log, logs/cache.log and logs/web.log.
  3. The shell creates a pipe from the grep process to the sort process.
  4. The shell forks two processes, which run at the same time; the next steps take them in turn. The grep process gets [grep] [ERROR] [logs/api.log] [logs/cache.log] [logs/web.log] and the sort process gets [sort].
  5. The grep process reads 4 lines from its files, writes 3 lines to the pipe and writes 1 error line to the terminal, then exits with status 2 because it could not read a file.
  6. The sort process reads 3 lines from the pipe and writes 3 lines to the terminal, then exits with status 0.
  7. The shell sets $? to 0, the status of the last command, the sort process. The earlier status 2 of the grep process does not reach $?.

set -o pipefail changes the rule: $? becomes the status of the rightmost command that failed, or 0 if none did. Run grep WARN archive/*.log | sort | wc -l with one unreadable log and pipefail on, and $? is 2, even though sort and wc -l both exit with 0. The option was added to POSIX in 2024 (POSIX.1-2024) after many shells already offered it, so a script for an older shell should check that it exists.

The same failure with pipefail on

Read the steps as text

  1. The shell reads the line "grep WARN archive/*.log | sort | wc -l". The shell option pipefail is set, so $? will be the rightmost non-zero status.
  2. The shell expands archive/*.log into archive/feb.log, archive/jan.log and archive/mar.log.
  3. The shell creates two pipes: one from the grep process to the sort process and one from the sort process to the wc process.
  4. The shell forks three processes, which run at the same time; the next steps take them in turn. Their argument lists are [grep] [WARN] [archive/feb.log] [archive/jan.log] [archive/mar.log], [sort] and [wc] [-l].
  5. The grep process reads 6 lines from its files, writes 3 lines to the pipe and writes 1 error line to the terminal, then exits with status 2 because it could not read a file.
  6. The sort process reads 3 lines from the pipe and writes 3 lines to the pipe, then exits with status 0.
  7. The wc process reads 3 lines from the pipe and writes 1 line to the terminal, then exits with status 0.
  8. With pipefail the shell sets $? to 2, the status of the grep process, the rightmost command that failed.

Signals

kill does not kill by itself. It sends a signal, and kill -TERM 4120 asks process 4120 to stop. The kernel delivers the signal, and what happens next depends on the process; the signals lesson explains how handlers should be written. If the process installed a handler for TERM, its own code runs. In this example the code cleans up and exits with status 0, and a handler may choose any status. If TERM has its default disposition, it ends the process. Bash reports a status of 128 plus the signal number: 143 for TERM, which is number 15 on Linux, and 130 for INT, number 2. Other shells can report signal termination differently.

TERM with and without a handler

Read the steps as text

  1. The worker and the logger are running. The worker has a handler that cleans up and exits with status 0, while the logger has no signal handler.
  2. The shell runs kill -TERM, so the kernel marks SIGTERM pending for the worker and the logger.
  3. The kernel delivers SIGTERM to the worker and the logger.
  4. The worker runs its handler, which cleans up. The logger has no handler, so the default action terminates it.
  5. The worker exits with status 0 after its cleanup. The shell reports status 143, which is 128 + 15, for the logger, killed by SIGTERM.

A process may instead ignore TERM and keep running. kill still succeeds, because it reports that the signal was sent, and ps -p 4120 shows whether the process is still there.

A process that ignores TERM

Read the steps as text

  1. The poller is running and has set SIGTERM and SIGINT to be ignored.
  2. The shell runs kill -TERM, so the kernel marks SIGTERM pending for the poller.
  3. The kernel delivers SIGTERM to the poller.
  4. The poller has set SIGTERM to be ignored, so the signal is discarded and it keeps running.

KILL is different: it cannot be caught, blocked or ignored, so the process gets no chance to clean up, and Bash on Linux reports 137, which is 128 + 9. That is why TERM comes first and KILL last.

A signal nobody can catch

Read the steps as text

  1. The importer and the poller are running. The importer has a handler that cleans up and exits with status 0, while the poller has set SIGTERM and SIGINT to be ignored.
  2. The shell runs kill -KILL, so the kernel marks SIGKILL pending for the importer and the poller.
  3. The kernel delivers SIGKILL to the importer and the poller.
  4. SIGKILL cannot be caught or ignored, so the importer and the poller are terminated whatever their handlers.
  5. The shell reports status 137, which is 128 + 9, for the importer and the poller, both killed by SIGKILL.

A long-running background process, called a daemon, is normally started and restarted by a service manager rather than by hand; that supervision is the subject of OS families, workloads and services.

Recap

  • The shell rewrites quotes and wildcards, opens redirections left to right and forks the processes, so a program sees only its final argument list.
  • A pipeline's $? is the last command's status by default and the rightmost failure with pipefail, which POSIX.1-2024 standardised.
  • A signal is a request: a handler can catch TERM and clean up, Bash reports 128 plus the signal number for default termination, and KILL cannot be caught.
Debugging a failed line

When a line misbehaves, ask which stage failed. Print the words the shell produced, for example with printf '%s\n' incoming/*.log, to check quoting and expansion. Run each command alone to see its own status. In Bash, inspect ${PIPESTATUS[@]} immediately after the pipeline to see each status; alternatively, turn on pipefail to expose the rightmost failure in $?. Check the redirection target's directory permissions. This order separates a shell mistake from a program or a permission failure.

Continue to OS families, workloads and services. Review User, kernel and system calls for the privilege boundary beneath these tools.