bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: command substitution is stripping set -e from options


From: Greg Wooledge
Subject: Re: command substitution is stripping set -e from options
Date: Fri, 2 Oct 2015 08:29:26 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Oct 02, 2015 at 02:09:21PM +0300, Christoph Gysin wrote:
> Since set -e does not work, it means I have to postfix every command
> with "|| exit $?":
> 
> f() {
>   some command || exit $?
>   more commands --with-args || exit $?
> }
> 
> output=$(f)

Since it's a function, I would recommend return instead of exit.  Also,
you don't need the $? there.  exit (or return) with no arguments will
retain the exit status of the previous command.

> My understanding was the the whole point of set -e was to have that
> behaviour by default for every command (with the documented
> exceptions).

That was the INTENT, perhaps, of the original designers way back in
the ancient Bourne shell days, but that is never how it WORKED in
real life.  The crazy shell command syntax just made it too quirky
and unreliable.  So they made exceptions, and then exceptions to the
exceptions, and so on until we have the current unusable mess.

Putting "|| return" or "|| exit" after all critical commands in your
script is precisely what you should do.  (Some people write a die()
function and then use "|| die 'my message'" instead.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]