[Top][All Lists]

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

Re: bug: return doesn't accept negative numbers

From: Linda Walsh
Subject: Re: bug: return doesn't accept negative numbers
Date: Thu, 11 Aug 2011 20:33:31 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: Gecko/20100228 Thunderbird/ Mnenhy/

` Stephane CHAZELAS wrote:
2011-08-08, 13:55(-07), Linda Walsh:
and both 'exit' and 'return' should return error "ERANGE" if "--posix" is
set, and -1 is given.  Iinvalid option doesn't make as much sense, in
this situtation, if it was -k or -m, sure...but in this case, it's a fact
that --posix artificially limits exit values apart from what is allowed in
most prog langs (which accept negative, but still return results &0xff),
so for Posix, it's a matter of disallowing a 'normal range', vs. it being
an invalid option....

POSIX doesn't prevent a shell from accepting -1 (or for doing
anything like eject a cd or output an error or turn red upon
"return -1"). It just says an *application* should not use
"return -1", that is that if one wants to write a portable
script, she shouldn't use "return -1".

Many POSIX shells accept "return -1"

$ ksh93 -c 'f() return -1; f; echo $?'
$ pdksh -c 'f() return -1; f; echo $?'
$ zsh -c 'f() return -1; f; echo $?'
$ posh -c 'f() return -1; f; echo $?'
return: invalid option -- '1'
$ posh -c 'f() return -- -1; f; echo $?'
$ mksh -c 'f() return -1; f; echo $?'
mksh: return: -1: unknown option
$ mksh -c 'f() return -- -1; f; echo $?'

But as you can see the result varies, so one shouldn't use
"return -1" if one wants to be portable accross POSIX shells.

Also note:

$ zsh -c 'f() return -1; f; echo $?'
$ zsh -c 'f() return -1; (f); echo $?'

That is even in shells that support arbitrary numbers for
return, as soon as they are cast to exit status, they are

Displaying -1 or 255 is fine. "-1" would be an "enhancement", but certainly not expected as it isn't clearly that the real value is an 8-bit integer...

Only the "broken" shells that attempt to validate options for return (despite, it being documented to have none), return invalid values.

I would deduce that it is only non-posix compliant shells that return '1', ... as they are trying to validate return's options (which doesn't take any)...

reply via email to

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