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 ebl...@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org