The Disappearing Script: Shebang Tricks and Magic Numbers
Open any script and the first line is probably #!/usr/bin/env bash. You’ve typed it so many times it’s muscle memory. The cursor lands, you type the shebang, and you move on.
But here’s a question worth pausing on: who actually reads that line?
Not the shell. Not Bash. The kernel.
The Magic Bytes
Ok, ok… that’s not completely true. If you run a script specifying the shell you want to use, as in:
File Descriptor Patterns: Beyond stdout and stderr
You’ve already used the STDIO redirection operators: <, >, 2>, 2>&1, and so on.
You may also have used the appending operators: >> and 2>>, and maybe the most modern ones: &> and &>>, to redirect both STDOUT and STDERR at the same time.
These operators cover most of what you need day-to-day. But have you ever wondered what they actually do — why > affects output, < affects input, and 2> affects errors? The answer is file descriptors, and understanding them turns a collection of punctuation into a coherent system.
The if Statement Secret: It Checks Exit Status, Not Booleans
Coming from almost any other programming language, you’d expect if to work on boolean expressions. You write something that evaluates to true or false, and the if decides which branch to take.
Bash works in a way that is almost, but not quite, entirely unlike that.
In Bash, if runs a command and checks its exit status. Zero means success (the “then” branch runs). Non-zero means failure (the “else” branch runs, if present).