Dualo
Bash / Shell Scripting

Pipes & redirections

Connect the output of one command to the input of another (`|`), or send it to a file (`>`, `>>`). The core composition tool of Unix.

1 min read

File descriptors: each process in Unix has a table of open file descriptors. FD 0 (stdin), 1 (stdout), 2 (stderr) are inherited from the parent (your shell). Redirections modify this table BEFORE exec'ing the command; the program itself just reads/writes to FDs without caring what they point to.

Pipe semantics: A | B creates an anonymous pipe (a kernel buffer). The shell forks both processes, connects A's FD 1 to the write end, B's FD 0 to the read end, then execs both. They run concurrently — A doesn't finish before B starts; B reads as A produces. This enables streaming over huge files without buffering everything in memory.

Pipe exit status: by default, $? is the exit code of the LAST command only. false | true exits 0. Use set -o pipefail to make the pipeline fail if any stage fails, or inspect ${PIPESTATUS[@]} for individual stages. Critical for set -e + pipelines.

Duplication syntax: 2>&1 means 'duplicate FD 2 to point where FD 1 points, right now'. Order matters: cmd >file 2>&1 first sends FD 1 to file, then copies that destination into FD 2 → both go to file. cmd 2>&1 >file first aims FD 2 at the terminal (current FD 1), then redirects FD 1 to file → stderr still on the terminal, stdout in file.

Process substitution <(cmd) and >(cmd): Bash-only (not POSIX). Creates a named pipe or /dev/fd/N and substitutes its path. Lets commands that want a FILE argument read from / write to another process. diff <(cmd1) <(cmd2) compares the outputs of two commands without temp files.

Here-docs & here-strings: <<EOF ... EOF feeds a block as stdin; <<<'text' feeds a string. Useful to hand-craft input without creating a file. Quote the marker (<<'EOF') to disable variable expansion inside.

Grounded on https://www.gnu.org/software/bash/manual/bash.html#Redirections

Next up

Variables & quoting

Store values, interpolate them, and — most importantly — quote them to survive spaces, globs, and special characters.