Created
January 21, 2025 22:28
-
-
Save overflowy/5bd7607a8971f8f641b1569b6bd49e88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# When a command fails, bash exits instead of continuing with the rest of the script. | |
set -o errexit | |
# This will make the script fail, when accessing an unset variable. | |
# When you want to access a variable that may or may not have been set, | |
# use "${VARNAME-}" instead of "$VARNAME", and you’re good. | |
set -o nounset | |
# This will ensure that a pipeline command is treated as failed, | |
# even if one command in the pipeline fails. | |
set -o pipefail | |
# This helps in debugging your scripts, a lot. Like, really lot. | |
# People can now enable debug mode, by running your script | |
# as TRACE=1 ./script.sh instead of ./script.sh. | |
# Also, use [[ ]] for conditions in if/else statements. | |
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 | |
# If appropriate, change to the script’s directory close to the start of the script | |
cd "$(dirname "$0")" | |
main() { | |
echo "Hello, World!" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment