Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active January 22, 2025 03:31
Show Gist options
  • Select an option

  • Save Offirmo/f7495d3ff7883c59b442625ac0cf4557 to your computer and use it in GitHub Desktop.

Select an option

Save Offirmo/f7495d3ff7883c59b442625ac0cf4557 to your computer and use it in GitHub Desktop.
[Bash -- template] #bash
## * #!/usr/bin/env bash is more portable than #!/bin/bash
#!/usr/bin/env bash
#
# This script does this and that.
## https://stackoverflow.com/questions/31313305/portably-trapping-err-in-shell-script
## http://mywiki.wooledge.org/BashFAQ/105
## TODO review those lines:
set -o errexit
set -eu -o pipefail
## * add set -eu -o pipefail
## * -e exits on failure of a simple command
## * -u exits on dereference of an undefined variable
## * -o pipefail exists on failures in pipe connected commands
## * put all commands in functions: this makes sure your script fully parses before anything executes;
## * pass all arguments to main()
# Prints a small usage help. This is called on `--help` and on parameter validation errors.
# Syntax follows http://docopt.org/.
usage() {
cat <<EOF
...
EOF
}
# Foos the bar.
foo() {
...
}
main() {
if [[ "$*" =~ ^(-h|--help)$ ]]; then
usage
exit
fi
foo
}
main "$@"
## current shell can be tested with this:
## https://stackoverflow.com/a/11097703/587407
echo "* current shell : '`ps -p $$ -ocomm=`'"
##########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment