$RodHat_

Topic · 20 articles

Unix Shell & Tooling

The shell is a programming language that punishes you for treating it like one, and most production scripts are one odd filename or one failed producer away from doing something stupid. The fixes are old, small, and almost never used.

Start with the four that stop scripts lying and deleting the wrong things: pipeline exit status, trap, null-terminated xargs, and flock. Then awk for the log questions you were about to write Python for, and the tools that keep a process alive or a connection warm.

Start here

Read these first, in this order.

  1. 01

    Tip · 2026-08-03

    Your shell pipeline succeeded because the last command was polite

    A failed producer can disappear behind a successful consumer. RodHat explains pipeline exit status, pipefail, PIPESTATUS, and how to stop backup scripts from lying.

  2. 02

    Tip · 2026-08-21

    Your shell scripts crash. trap EXIT is why the mess does not have to survive.

    The shell trap builtin intercepts signals and the EXIT pseudo-signal to run cleanup code regardless of how a script dies. Most scripts skip it entirely. Here is how to use it correctly, including the subshell gotchas and the ERR trap edge cases that bite people.

  3. 03

    Tip · 2026-07-24

    find | xargs is a filename injection bug you've been shipping for years

    Whitespace, newlines and quotes in filenames turn the standard find-pipe-xargs idiom into something that deletes the wrong files. -print0 and -0 fix it, find -exec + fixes it better, and there's one case where only one of them works.

  4. 04

    Tip · 2026-08-09

    Your cron job is running twice and that's why the numbers are wrong

    A job that usually takes four minutes on a five-minute schedule will eventually take six, and then you have two copies racing. flock fixes it in one line, and the pidfile you were about to write instead is broken in ways flock isn't.

  5. 05

    Tip · 2026-08-01

    awk has hash maps, and that's why you don't need the Python script

    Most people's awk stops at print $7. Associative arrays, END blocks and a two-line accumulator turn it into the right tool for ninety percent of the log questions people currently write a script for, and it runs on a box with nothing installed.

  6. 06

    Tip · 2026-08-18

    nohup, disown, setsid: what each actually does and which one you want

    Three tools for keeping a process alive after you close the terminal. They do not do the same thing. Here is the kernel-level difference, and when each one applies.

The syscalls underneath

Version introduced, sourced from the man page. Full cards in the syscall reference, or check your kernel.

InterfaceSinceWhat it does
flock(2)4.2BSD; Linux 2.0Apply or remove an advisory whole-file lock tied to an open file description.

More console tips (5)

Rod's tales (4)

MOTD (5)

Other topics