From owner-freebsd-current@FreeBSD.ORG Fri Nov 7 13:59:59 2014 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 287B582C for ; Fri, 7 Nov 2014 13:59:59 +0000 (UTC) Received: from smtprelay04.ispgateway.de (smtprelay04.ispgateway.de [80.67.29.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DA03A875 for ; Fri, 7 Nov 2014 13:59:58 +0000 (UTC) Received: from [78.35.130.26] (helo=fabiankeil.de) by smtprelay04.ispgateway.de with esmtpsa (TLSv1.2:AES128-GCM-SHA256:128) (Exim 4.84) (envelope-from ) id 1Xmjo5-0004rM-7g; Fri, 07 Nov 2014 14:42:29 +0100 Date: Fri, 7 Nov 2014 14:42:30 +0100 From: Fabian Keil To: Eric van Gyzen Subject: Re: sh: "local" assignment from command loses exit status Message-ID: <63396928.4e2735a2@fabiankeil.de> In-Reply-To: <545BB08D.4020402@vangyzen.net> References: <545BADDE.5050605@vangyzen.net> <74dd7d12.793ea654@fabiankeil.de> <545BB08D.4020402@vangyzen.net> Reply-To: Freebsd current MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; boundary="Sig_/b/5uYkxMzcmbV0TC=/jDNh8"; protocol="application/pgp-signature" X-Df-Sender: Nzc1MDY3 Cc: Freebsd current X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Nov 2014 13:59:59 -0000 --Sig_/b/5uYkxMzcmbV0TC=/jDNh8 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Eric van Gyzen wrote: > On 11/06/2014 12:30, Fabian Keil wrote: > > Eric van Gyzen wrote: > > > >> In sh, if I use a single statement to declare a local variable and > >> assign the output of a command to it, the exit status of that command = is > >> lost. For example: > >> > >> should_return_false() { > >> local var1=3D`false` > >> } > >> > >> The function should return non-zero, but it returns zero. > > The function should return the return code of the last command. > > In your example, the last command is "local". >=20 > Fair enough. What about errexit? The shell ran a command whose exit > status was not tested, that status was failure, yet the shell did not exi= t. That's unrelated to the "local", though. Compare: fk@r500 ~ $sh -e -c 'true `false; echo "Not reached"`; echo Reached' Reached While it's not obvious from the man page[1], my assumption is that this is intentional behaviour. The return code of the command substitution subshell can't be checked in the parent shell, as $? belongs to the command that gets the output as argument (in your case "local"), thus counting this as an untested return code would be inconvenient. Fabian [1] It could be argued that the behaviour is documented as "-e ... tends to behave in unexpected ways", though ... --Sig_/b/5uYkxMzcmbV0TC=/jDNh8 Content-Type: application/pgp-signature Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iEYEARECAAYFAlRczEYACgkQBYqIVf93VJ0bWQCgpOk34LB5cg7Js/FNCvyrW2/v OSwAnAqTxfXWDLsxXeZtidfW6KIjsm1J =YS98 -----END PGP SIGNATURE----- --Sig_/b/5uYkxMzcmbV0TC=/jDNh8--