Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Arithmetic Zero
- X-seq: zsh-users 29718
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: sergio <sergio@xxxxxxxxxxxxx>, zsh-users@xxxxxxx
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: Arithmetic Zero
- Date: Tue, 5 Mar 2024 07:09:08 +0000
- Archived-at: <https://zsh.org/users/29718>
- In-reply-to: <CAH+w=7ajHGSDi8pR5qb8k404UJ8ZybgXgyPdQ5PzvPRxccOm9A@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: sergio <sergio@xxxxxxxxxxxxx>, zsh-users@xxxxxxx, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- References: <47117922-e17f-44d1-8a2f-2cb4a9c53606@outerface.net> <CAH+w=7ajHGSDi8pR5qb8k404UJ8ZybgXgyPdQ5PzvPRxccOm9A@mail.gmail.com>
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