Path: csiph.com!au2pb.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.glorb.com!usenet.stanford.edu!not-for-mail From: Eric Blake Newsgroups: gnu.bash.bug Subject: Re: command substitution is stripping set -e from options Date: Thu, 1 Oct 2015 13:04:58 -0600 Organization: Red Hat, Inc. Lines: 85 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="gVi8wbd8nX35s12LKMtKp3084DckWNuvs" X-Trace: usenet.stanford.edu 1443727343 12681 208.118.235.17 (1 Oct 2015 19:22:23 GMT) X-Complaints-To: action@cs.stanford.edu To: Christoph Gysin , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org Openpgp: url=http://people.redhat.com/eblake/eblake.gpg X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11570 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --gVi8wbd8nX35s12LKMtKp3084DckWNuvs Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 10/01/2015 04:24 AM, Christoph Gysin wrote: > It seems that set -e is stripped from the options ($-) when executing > commands with command substitution: >=20 > $ bash -euc 'echo $-; f(){ false; echo $->&2; }; x=3D$(f)' > ehuBc > huBc >=20 > I would expect the shell to exit as soon as it executes 'false'. Your expectations may be noble, but that's not how 'set -e' works. >=20 > Is this intended? Is it documented somewhere? Whether e disappears from $- may be unintended, but what IS documented is that there are contexts in which set -e has no effect, and when in one of those contexts, you cannot re-enable set -e. One such context is on the left side of && or ||. Even more non-intuitively, if you have a function that tries to use set -e, the effect of set -e depends on the context of where the function is called (that is, 'f' may behave differently than 'f || true'). >=20 > I'm trying to catch errors in shellscripts by starting them with: >=20 > set -euo pipefail Sadly, it won't do what you think. Using 'set -e' is almost NEVER a good idea, because it forces code contortions for things that are meant to have non-zero status, while simultaneously missing cases that you intuitively want it to catch, and such behavior is mandated by POSIX for compatibility. Reading the archives of this list will find numerous threads where we recommend against using 'set -e' - it exists only for historical back-compat, and not for useful work. >=20 > It seems now that this is not enough, I would have to start every > command substitution with set -e: >=20 > var=3D$(set -e; command1; command2) Won't do what you want, so don't expect it to. >=20 > It *seems* to work with only a single command, because the return > value of the assignment is the last command executed inside the > command substitution. But if there are multiple commands, or a > function call, errors are not caught. What's wrong with: var=3D$(command1 && command2) || exit without having to rely on set -e? --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --gVi8wbd8nX35s12LKMtKp3084DckWNuvs Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWDYPaAAoJEKeha0olJ0NqyawH/iIxbrwyHOF51KxOW7kYxLY3 rT1CV3jBzvBicUPdGUIlWyf75+LdBvhRAX4xzX5hD7nDOy3lGe+aomcIucuFpOK1 aWINSila9mGJoY+0NubLnplI4elpu476sZGr279qNL0ARkV3X+K/p3b59atsWiQK IlJ/5XKuZtVuQpB5tFD7Bvuzkt+G6tV1qcqLFrcYly64pJJvah2Zd69kFkcZtn1d 3umOv5lNDuyhSmR6gwNnab3zGdeT5OCK/toPDat37dCZjeeHC+iP/Cetkl/igfJq gbTRGvLF9m4Yed2Kx7xaeIcjiwci1+sqRLzVs0ekakG5en77+VlplH8GgzMCzuY= =XrEH -----END PGP SIGNATURE----- --gVi8wbd8nX35s12LKMtKp3084DckWNuvs--