zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: sergio <sergio@outerface.net>, zsh-users@zsh.org
Cc: Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: Arithmetic Zero
Date: Tue, 5 Mar 2024 07:09:08 +0000	[thread overview]
Message-ID: <20240305070908.rxv4tc6at57arb6t@chazelas.org> (raw)
In-Reply-To: <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@outerface.net> 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


  parent reply	other threads:[~2024-03-05  7:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-05  3:28 sergio
2024-03-05  4:23 ` Bart Schaefer
2024-03-05  5:02   ` Lawrence Velázquez
2024-03-05  7:09   ` Stephane Chazelas [this message]
2024-03-05  7:26 ` Roman Perepelitsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240305070908.rxv4tc6at57arb6t@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=schaefer@brasslantern.com \
    --cc=sergio@outerface.net \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).