1

In my shell script, I'm calling another perl script, when I use "exit -1" in perl script, my parent shell script receives 65280, not -1 why?

  • Does this answer your question? [Perl system() call failed with return code 65280](https://stackoverflow.com/questions/11989196/perl-system-call-failed-with-return-code-65280) – Nick ODell Jul 23 '22 at 03:21
  • To explain a bit more: -1 gets turned into 255 because exit codes are only 8 bits on Linux, and 255 the unsigned version of -1. Then perl system() call returns the exit code left shifted by eight bits. – Nick ODell Jul 23 '22 at 03:22
  • 1
    The *shell* shows that as perl's exit value? Everyone is assuming you're using `system` from perl but it sounds like that isn't the case? – Shawn Jul 23 '22 at 04:41
  • You should not use negative number as exit code, operating systems expect return code to be positive to indicate exit status. For more detailed information read documentation on your operating system. – Polar Bear Jul 23 '22 at 06:00

1 Answers1

4

As per the documentation for system,

  1. If $? == -1, then there was a problem launching the process, and $! contains the error code.

    This was not the case here.

  2. If $? & 0x7F is true, the process was killed by that signal.

    65280 & 0x7F == 0xFF00 & 0x7F == 0

    The script wasn't killed by a signal.

  3. Otherwise, the program exited with $? >> 8.

    65280 >> 8 == 0xFF00 >> 8 == 0xFF == 255.

    The program exited with exit code 255.

Why not -1? Well, Linux exit codes must be in [0,255]. Using -1 is incorrect, and gets interpreted as 255.

While I directed you to the docs for system, this is what the system provides so it's not specific to Perl.

ikegami
  • 367,544
  • 15
  • 269
  • 518