Skip to content

Instantly share code, notes, and snippets.

@crizCraig
Last active October 27, 2022 20:30
Show Gist options
  • Save crizCraig/f42bc250754bed764ada5f95d101dbea to your computer and use it in GitHub Desktop.
Save crizCraig/f42bc250754bed764ada5f95d101dbea to your computer and use it in GitHub Desktop.
bash boilerplate
#!/usr/bin/env bash
set -e # Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
set -u # Attempt to use undefined variable outputs error message, and forces an exit
set -x # Similar to verbose mode (-v), but expands commands
set -o pipefail # Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# end boilerplate from: https://gist.github.com/crizCraig/f42bc250754bed764ada5f95d101dbea
@neduma
Copy link

neduma commented Feb 1, 2020

please consider

    set -o errexit
    set -o nounset
    set -o noglob
    set -o pipefail

@crizCraig
Copy link
Author

crizCraig commented Oct 27, 2022

From https://sharats.me/posts/shell-script-best-practices/

#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
    set -o xtrace
fi

if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
    echo 'Usage: ./script.sh arg-one arg-two

This is an awesome bash script to make your life better.

'
    exit
fi

cd "$(dirname "$0")"

main() {
    echo do awesome stuff
}

main "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment