Created
April 16, 2020 08:36
-
-
Save duraki/4373a53a0bd1a242e48b30411c7f7607 to your computer and use it in GitHub Desktop.
The first two statements of your BASH script should be…
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 | |
set -euo pipefail |
#!/bin/sh
# Author:
# License: Unlicense
set -euf
log() {
printf '\033[32m->\033[m %s\n' "$*"
}
die() {
log "$*" >&2
exit 1
}
usage() {
echo "${0##*/} ARGS
desc
"
exit 0
}
Valid template for shell scripts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first statement is a Mac, GNU/Linux, and BSD portable way of finding the location of the bash interpreter.
The second statement combines:
set -e
which ensures that your script stops on first command failure. By default, when a command fails, BASH executes the next command. Looking at the logs, you might feel that the script executed successfully while some commands might have failed. Caveat: Be careful about applying it to existing scripts.set -u
which ensures that your script exits on the first unset variable encountered. Otherwise, bash replaces the unset variables with empty default values.set -o pipefail
which ensures that if any command in a set of piped commands failed, the overall exit status is the status of the failed command. Otherwise, the exit status is the status of the last command.