grep -rl "scripting" ./blog

Bash Scripting Guides

Writing scripts that survive contact with real input: control flow, functions, quoting, and the shell behaviors that cause silent breakage.

19 articles

Debugging Bash Scripts: set -x, PS4, and ShellCheck How to find out what a misbehaving shell script is actually doing: syntax checks with bash -n, tracing with set -x and a better PS4, tracing to a file, catching errors with trap, and finding bugs before they run with ShellCheck. getopts Explained: Parsing Script Arguments Without Regret Reading positional parameters works until someone passes flags in a different order. getopts is built into bash and handles short options properly, getopt handles long ones, and there is a reason to avoid one of them. Shell Completion: How It Works and How to Write Your Own Why Tab knows your command options, where completion scripts live, and writing one for your own tool in bash and zsh. trap and Signal Handling in Shell Scripts Cleaning up temporary files on exit, handling Ctrl+C properly, and why EXIT is the only trap most scripts need. Bash Quoting Explained: The Single Biggest Source of Shell Bugs Word splitting and glob expansion on unquoted variables cause more shell breakage than any other single mistake. Here is exactly what each quoting style does and when to use it. Exit Codes Explained: What $? Is Telling You Every command returns a number. Zero means success and everything else means something specific, including the codes that encode which signal killed a process. Heredocs and Process Substitution in Bash Feed multi-line text to a command without a temporary file, and present a command's output where a filename is expected. Two syntaxes that remove most of the need for temporary files in scripts. Bash Arrays and Parameter Expansion Arrays handle lists of things without word splitting bugs, and parameter expansion does substring, default, and pattern operations without launching sed. Both remove whole categories of shell mistakes. seq Command Explained: Generating Number Sequences seq prints a range of numbers, with control over step, padding, and separator. It drives loops, generates test data, and builds padded filenames in one line. basename and dirname Explained: Splitting Paths in Scripts Two small commands that take a path apart reliably. They handle the edge cases that string manipulation gets wrong, and knowing the shell alternatives is worth as much as knowing the commands. realpath and readlink Explained: Resolving Symlinks and Relative Paths Turn a messy relative path full of symlinks and dot-dot components into the one canonical absolute path. Essential whenever a script must know where it really is. Bash Error Handling: set -euo pipefail and Why Your Script Needs It By default a bash script that fails halfway through keeps running as if nothing happened. Three options change that, and knowing exactly what each one does is the difference between safety and mystery. jq Explained: Querying JSON From the Command Line APIs, container tooling, and cloud CLIs all speak JSON, and grep is the wrong tool for structured data. jq is a small language for filtering and reshaping it, and a handful of patterns cover most real use. Bash Scripting Part 2: Conditionals, Loops, and Functions Your first script ran commands top to bottom. Real scripts make decisions, repeat work, and organize into functions. This is the control-flow layer of bash: if, test brackets, for, while, case, and functions with local variables. The xargs Command Explained: Turning Output Into Arguments xargs takes lines of input and turns them into command arguments, letting you chain find, grep, and other tools into pipelines that act on hundreds of files at once. Here is how it works and where it bites. The awk Command Explained awk is a field-based text processing language built into every Linux system, ideal for extracting columns, summing values, and reformatting structured text. This guide covers its pattern-action model, built-in variables, and common one-liners. The sed Command Explained sed is the standard Linux stream editor for find-and-replace, line deletion, and text transformation from the command line. This guide covers substitution syntax, in-place editing, addressing specific lines, and common real-world patterns. Writing Your First Bash Script A practical introduction to Bash scripting: the shebang line, variables, arguments, conditionals, loops, and functions, with runnable examples you can adapt for real automation tasks. Writing Scheduled Scripts That Actually Work on Linux Scripts that run fine interactively often fail silently when triggered by cron or systemd timers. This guide covers the practical differences: environment variables, working directories, logging, locking, and failure notification for scripts meant to run unattended.