0

I want my bash scripts to detect execution errors and exit.

For the longest time I've been using the try/yell/die approach as seen here.

yell() { echo "$0: $*" >&2; }
die() { echo -e "\e[31m$*\e[0m"; exit 1; }
try() { "$@" || die "cannot $*"; }

However this requires me to wrap my commands line this;

try curl https://blah.com | try bash

It seems the better approach would be to use;

set -e
set -o pipefail
set -E

Are there any downsides to using the set approach, opposed to try/yell/die?

2

2 Answers 2

1

See Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected? for some related discussion. – Tom Fenech

Sign up to request clarification or add additional context in comments.

Comments

1

FYI:

I use ...

#!/usr/bin/env bash

shout() { echo "$0: $*" >&2; }
die() { shout "${@:2} ($1)"; exit $1; }
try() { "$@" || die $? "cannot $*"; }

2 minor differences:

  1. The exit code we eventually receive is the one that was generated by the command being tried.
  2. The shout includes the exit code after the command in (, ).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.