Revision: 715 http://svn.savannah.gnu.org/viewvc/?view=rev&root=administration&revision=715 Author: iank Date: 2023-12-13 16:40:22 -0500 (Wed, 13 Dec 2023) Log Message: ----------- formatting, improve preamble, fix path
Modified Paths: -------------- trunk/sviki/fsf/bash-style-guide.mdwn Modified: trunk/sviki/fsf/bash-style-guide.mdwn =================================================================== --- trunk/sviki/fsf/bash-style-guide.mdwn 2023-12-13 13:12:53 UTC (rev 714) +++ trunk/sviki/fsf/bash-style-guide.mdwn 2023-12-13 21:40:22 UTC (rev 715) @@ -239,6 +239,8 @@ For scripts with no functions add: ``` +if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi +shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR ``` @@ -315,8 +317,8 @@ Note that `var=` as shown above would be a global variable. In a function body, it is best to declare variables as function-local, as noted in the 'Variables:' section. However, don't set the variable like -`local x=3` because that has the same problem. Instead, `local x; -x=3`. Shellcheck notices this as warning SC2155. If the variable should +`local x=$(cmd)` because that has the same problem. Instead, `local x; +x=$(cmd)`. Shellcheck notices this as warning SC2155. If the variable should be declared with `local`, `declare`, `readonly`, etc, declare it without a value, then set it's value separately (eg: `local var`, or `local tmp` in the previous example). @@ -429,7 +431,7 @@ rm -v ./* # a file could be named -rf ``` -Avoid `set -u`. Empty vars are useful. Check for them with [[ $var ]]. `set +Avoid `set -u`. Empty vars are useful. Check for them with `\[[ $var ]]`. `set -u` only leads to explicitly setting variables to empty, and other hackery to avoid unset errors - a complication which is not worth the trouble. `shellcheck` will find completely unset variables. @@ -449,7 +451,7 @@ Use GNU getopts whenever there is more than one option, or an option and an argument. Reference info at: -/usr/share/doc/util-linux/examples/getopt-parse.bash +/usr/share/doc/util-linux/examples/ ``` usage() { @@ -462,7 +464,7 @@ Note: Uses GNU getopt options parsing style EOF - exit $1 + exit $1 } ##### begin command line parsing ########