[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
- Re: bug: return doesn't accept negative numbers, (continued)
- Re: bug: return doesn't accept negative numbers, Jon Seymour, 2011/08/11
- Re: bug: return doesn't accept negative numbers, Chet Ramey, 2011/08/07
- Re: bug: return doesn't accept negative numbers, Eric Blake, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Linda Walsh, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Chet Ramey, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Mike Frysinger, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Chet Ramey, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Linda Walsh, 2011/08/08
- Re: bug: return doesn't accept negative numbers, Eric Blake, 2011/08/09
- Re: bug: return doesn't accept negative numbers, Chet Ramey, 2011/08/09
- Message not available
- Re: bug: return doesn't accept negative numbers,
Stephane CHAZELAS <=
- Re: bug: return doesn't accept negative numbers, Linda Walsh, 2011/08/11
Re: bug: return doesn't accept negative numbers, Dennis Williamson, 2011/08/05