Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Arithmetic Zero



2024-03-04 20:23:51 -0800, Bart Schaefer:
> On Mon, Mar 4, 2024 at 7:28 PM sergio <sergio@xxxxxxxxxxxxx> wrote:
> >
> > I believe -e is best practices for scripts
> 
> Leaving opinions aside ...
[...]

https://mywiki.wooledge.org/BashFAQ/105
https://fvue.nl/wiki/Bash:_Error_handling
https://mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail

(for bash but most of it applies to other Bourne-like shells)
are good references about the problems with errexit.

One might add that using option names rather than letters is
preferable as more legible (and more portable like for -f vs
noglob where -f means something different in zsh and other
shells), and that it's preferable to set them via "set" rather
than via the shebang as the shebang is not honours when the
script is invoked as zsh path/to/the/script or source
path/to/the/script or zsh < path/to/the/script and on most
systems, shebang can have only one extra argument and using
options there prevents adding the option delimiter.

So:

#! /bin/zsh -
set -o errexit -o nounset -o pipefail

(or setopt if you prefer but I like set -o better as that's
supported by many other shells)

Rather than

#! /bin/zsh -euopipefail

Of those 3 options, nounset is the least controversial. errexit
is a minefield and pipefail can be problematic when using
pipe consumers that exit (successfully) without reading their
whole input (where a death by SIGPIPE of the feeder is not
necessarily a failure.

Best to do proper error handling. I usually define a die
function for that:

die() {
  print -ru2 -C1 -- "$@"
  exit 1
}

cmd1 || die
cmd2 || die "some error message"

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author