bug-bash
[Top][All Lists]
Advanced

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

Re: bug: return doesn't accept negative numbers


From: Eric Blake
Subject: Re: bug: return doesn't accept negative numbers
Date: Fri, 05 Aug 2011 18:08:19 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11

On 08/05/2011 05:41 PM, Linda Walsh wrote:

I guess I don't use negative return codes that often in shell, but
I use them as exit codes reasonably often.

'return' barfs on "return -1"...

Since return is defined to take no options, and ONLY an integer,
as the return code, it shouldn't be hard to fix.

According to POSIX, it's not broken in the first place. Portable shell is requires to pass an unsigned decimal integer, no greater than 255, for defined behavior.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#return


Seem to fail on any negative number, but 'exit status' is defined
as a short int -- not an unsigned value (i.e. -1 would return 255).

In bash, 'return -- -1' sets $? to 255 (note the --). But since that is already an extension (POSIX does not require 'return' to support -- any more than it is required to support an argument of -1), I agree with your argument that bash would be more useful if, as an extension to POSIX, it would handle 'return -1' - in fact, that would match ksh behavior. Conversely, since portable code already can't use it, it's no skin off my back if nothing changes here.

$ bash -c 'f() { return -- -1; }; f; echo $?'
255
$ bash -c 'f() { return  -1; }; f; echo $?'
bash: line 0: return: -1: invalid option
return: usage: return [n]
2
$ dash -c 'f() { return -- -1; }; f; echo $?'
return: 1: Illegal number: --
$ dash -c 'f() { return  -1; }; f; echo $?'
return: 1: Illegal number: -1
$ ksh -c 'f() { return -- -1; }; f; echo $?'
255
$ ksh -c 'f() { return  -1; }; f; echo $?'
255
$

--
Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org



reply via email to

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