Sheet ⁨05⁩ · ⁨Code snippets⁩Surveyed ⁨2026⁩
Category
2Snippets

Bash

Bash Argument Parsing with getopts: Build CLI Scripts with Flags and Options

Bash Argument Parsing with getopts: Build CLI Scripts with Flags and Options

Quick Tip getopts is a shell builtin that parses short flags properly, including grouped ones like -vf. Here is the full pattern, including the shift everybody forgets and the `local OPTIND

Bash Script Locking: Prevent Concurrent Runs with a PID File

Bash Script Locking: Prevent Concurrent Runs with a PID File

Quick Tip Wrap any script that must not run twice at once in a PID-file lock, and a second copy simply exits instead of corrupting your data. The Problem The Problem Cron does not c