Shell snippets

The snippets on this site are intended for getting copied and being useful so please do that.

Argument parser

A simple argument parser for bash.

#!/bin/bash

# Settings variables go here
FOO="foo"

show_help() {
cat <<EOF
Usage: foo <<options>> <bar>

Blubber blah blah ...
EOF
}

while [[ "$#" -gt 0 ]]; do
        case "$1" in
                --foo) FOO="$2"; shift 2;;
                
                --help) show_help; exit 0;;
                *) printf "Unknown option: %s\n" "$1"; exit 1;;
        esac
done

# Logic goes here
echo "$FOO"