wscott44 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks: Why is it difficult to return a negative exit code to the UNIX Korn shell (on AIX) and see it as I expect it? /tmp> perl -e 'exit -2' ; echo $? 254 /tmp> perl -e 'exit -1' ; echo $? 255 /tmp> perl -e 'exit -111' ; echo $? 145 /tmp> perl -e 'exit -123' ; echo $? 133 Thank you, Wayne

Replies are listed 'Best First'.
Re: negative exit codes?
by almut (Canon) on Jun 05, 2008 at 21:11 UTC

    Unix is using an 8-bit value as exit/return code (see man wait). This value is typically interpreted as an unsigned value by the shell.

    Update: but you could use Perl to display it properly :)  For example

    $ perl -e 'exit -111' $ perl -e "print unpack('c',pack 'C', $?),\$/" -111

    (possible range -128 .. 127)

      Thank you to those who replied so promptly. It's all clearer now. As for using Perl to display it properly... that's what I thought :-D Regards, Wayne
Re: negative exit codes?
by pc88mxer (Vicar) on Jun 05, 2008 at 21:07 UTC
    I am pretty sure exit status codes are limited to 8 bits -- see this Wikipedia article: Exit status

    That's why a status of -1 is showing up as 255.

    Trying this: bash -c 'exit -1' produces the error Illegal number: -1, so I doubt status codes are meant to be interpreted as signed integers.