Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14781

exiting noninteractive shells on 'shift 2'

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Eric Blake <eblake@redhat.com>
Newsgroups gnu.bash.bug
Subject exiting noninteractive shells on 'shift 2'
Date Thu, 8 Nov 2018 14:37:11 -0600
Organization Red Hat, Inc.
Lines 50
Approved bug-bash@gnu.org
Message-ID <mailman.3668.1541710058.1284.bug-bash@gnu.org> (permalink)
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace usenet.stanford.edu 1541710059 4561 208.118.235.17 (8 Nov 2018 20:47:39 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash <bug-bash@gnu.org>
Envelope-to bug-bash@gnu.org
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0
Content-Language en-US
X-Scanned-By MIMEDefang 2.79 on 10.5.11.15
X-Greylist Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 08 Nov 2018 20:37:12 +0000 (UTC)
X-detected-operating-system by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From 209.132.183.28
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14781

Show key headers only | View raw


If I'm reading POSIX correctly, shift is a special built-in utility, and 
if '$#' is 0 or 1, then 'shift 2' counts as a utility error that shall 
exit the shell, per the table in 2.8.1 Consequences of Shell Errors:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_01


dash gets this right:

$ dash -c 'set 1
 > shift 2
 > echo "oops"'
dash: 2: shift: can't shift that many

but bash happily lets 'shift 2' fail with $? set to 1 but continues on 
with execution of the rest of the script, even when in POSIX mode:

$ bash -c 'set 1
 > shift 2
 > echo "oops"'
oops
$ bash -c 'set 1; set -o posix
 > shift 2
 > echo "oops"'
oops

I spent more than an hour today figuring out why my shell script was 
inf-looping, and traced it back to failure to check for non-zero status 
from 'shift 2', where I had been expecting immediate hard failure of the 
entire script.  I don't care if bash doesn't hard-exit when not in POSIX 
mode, and it must not hard-exit when in interactive use; but the 
non-interactive use not doing a hard exit seems like a bug.

On the other hand, the POSIX wording for shift, under EXIT STATUS, 
weakens the "shall fail" from the earlier table into a weaker "may fail":

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#shift

     If the n operand is invalid or is greater than "$#", this may be 
considered a syntax error and a non-interactive shell may exit; if the 
shell does not exit in this case, a non-zero exit status shall be 
returned. Otherwise, zero shall be returned.

Tested on Fedora 28 with bash-4.4.23-1.fc28.x86_64

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

exiting noninteractive shells on 'shift 2' Eric Blake <eblake@redhat.com> - 2018-11-08 14:37 -0600

csiph-web