From owner-freebsd-current@FreeBSD.ORG Thu Nov 6 17:20:32 2014 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 37F4E90E; Thu, 6 Nov 2014 17:20:32 +0000 (UTC) Received: from smtp.vangyzen.net (hotblack.vangyzen.net [IPv6:2607:fc50:1000:7400:216:3eff:fe72:314f]) by mx1.freebsd.org (Postfix) with ESMTP id 1C85DC86; Thu, 6 Nov 2014 17:20:32 +0000 (UTC) Received: from marvin.lab.vangyzen.net (c-24-125-214-90.hsd1.va.comcast.net [24.125.214.90]) by smtp.vangyzen.net (Postfix) with ESMTPSA id 3C5B256467; Thu, 6 Nov 2014 11:20:31 -0600 (CST) Message-ID: <545BADDE.5050605@vangyzen.net> Date: Thu, 06 Nov 2014 12:20:30 -0500 From: Eric van Gyzen User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:31.0) Gecko/20100101 Thunderbird/31.0 MIME-Version: 1.0 To: Freebsd current , jilles@FreeBSD.org Subject: sh: "local" assignment from command loses exit status Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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: Thu, 06 Nov 2014 17:20:32 -0000 Jilles and -current: 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=`false` } The function should return non-zero, but it returns zero. This becomes especially apparent when using the errexit option (-e flag), since the shell should exit, but it does not. Splitting the declaration and assignment into two lines works around the [suspected] bug. A more complete example follows. Cheers, Eric #!/bin/sh returns_false() { var1=`false` } if returns_false; then echo 1:FAIL else echo 1:PASS fi should_return_false() { local var1=`false` } if should_return_false; then echo 2:FAIL else echo 2:PASS fi workaround_returns_false() { local var1 var1=`false` } if workaround_returns_false; then echo 3:FAIL else echo 3:PASS fi set -o errexit trap 'echo 4:PASS' EXIT should_return_false trap '' EXIT echo 4:FAIL # because the shell should have exited