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: Stephane CHAZELAS
Subject: Re: bug: return doesn't accept negative numbers
Date: Mon, 8 Aug 2011 22:27:45 +0100
User-agent: slrn/pre1.0.0-18 (Linux)

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 $?'
255
$ pdksh -c 'f() return -1; f; echo $?'
-1
$ zsh -c 'f() return -1; f; echo $?'
-1
$ posh -c 'f() return -1; f; echo $?'
return: invalid option -- '1'
1
$ posh -c 'f() return -- -1; f; echo $?'
-1
$ mksh -c 'f() return -1; f; echo $?'
mksh: return: -1: unknown option
1
$ mksh -c 'f() return -- -1; f; echo $?'
-1

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 $?'
-1
$ zsh -c 'f() return -1; (f); echo $?'
255

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

-- 
Stephane




reply via email to

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