🛠️
Notes
  • README
  • ansible
    • Literal curly braces (raw)
  • bash
    • Case statement
    • Change quote style mid-string
    • Comparing versions
    • Hiding credentials on the command line
    • Directory of script
    • Using find to run a command on multiple files
    • Getting the line number of the nth match
    • Getopts
    • Parsing output with long lines using less
    • Print line at number
    • Remove final newline
    • Reading content between markers
    • Determine if a script was sourced or executed
    • Bash substring
    • Run a function on interrupt or error
    • Reference variable by name
  • chrome
    • Bypassing Chrome's NET::ERR_CERT_INVALID page
  • css
    • box-sizing: border-box
    • The currentColor keyword
    • Wrapper taking up at least 100% height
    • Using margin: auto; for centering
    • Margin-collapse
    • Which unit to use?
    • Hiding elements
  • git
    • Conditional git config includes
    • Viewing the evolution of a line or function
    • Name of current branch
    • Get the path to the repository root
    • Replaying a set of changes on a specific branch
    • Hide file from git diff output
    • Listing untracked files with git status
  • github
    • Setting up GitHub Actions
  • groovy
    • Appending items to a list
  • kubernetes
    • Waiting for a pod to be ready
  • make
    • Passing arguments to make rules
    • Running make in a set of subdirectories
  • npm
    • Update a value in a project's .npmrc
  • podman
    • Target last container
  • rust
    • The match operator
    • Unwrapping a Result
  • tmux
    • New window with prompt
  • tools
    • Running ngrok in the background
    • Using entr to react to file changes
  • typescript
    • Inferring the type of elements in an array
  • vim
    • The command-line window
    • Populate quickfix list with eslint errors
    • Visual increment
    • Opening a list of files in split windows
    • Insert line above matched line
    • Spelling
    • The tabular plugin
    • Populate quickfix list with tsc errors
  • yaml
    • Yaml multiline strings
  • zsh
    • Lazy loading command setup
Powered by GitBook
On this page

Was this helpful?

  1. bash

Getopts

Here's a useful pattern for collecting command line options in a script:

ERROR="Bad usage, see ${0##*/} -h"

read -r -d "" USAGE <<EOF
Short description

Usage: ${0##*/} [-fh]
  -f ARG      Do something
  -h          Show usage

Example:
  ${0##*/} -f my-arg

EOF

if [ "$1" = "--help" ]; then
  echo "$USAGE" && exit 0
fi

while getopts f:h opt; do
  case $opt in
    f) VAR=$OPTARG                         ;;
    h) echo "$USAGE" && exit 0             ;;
    *) echo "$ERROR" && exit 1             ;;
  esac
done

I like to combine this with a check for superfluous positional arguemnts:

POS_ARG=${*:$OPTIND:1}

OTHER_ARGS=${*:$OPTIND+1}

if [ -n "$OTHER_ARGS" ]; then
  echo "ERROR: Unprocessed positional arguments: $OTHER_ARGS"
  exit 1
fi

If you only expect a specific number of positional arguments, this is a good safety measure, because it reduces the risk of missing important options. Because getopts will stop processing options as soon as it hits a positional argument, in a case like this:

$ ./my_script.sh -f f_arg pos_arg -h

the -h flag will be ignored. Imagine it being a dry-run toggle, for example - better to quit than to miss that.

PreviousGetting the line number of the nth matchNextParsing output with long lines using less

Last updated 4 years ago

Was this helpful?