-1
#!/usr/bin/env bash
# set -x

readonly err_code=256

my_function () {
    return $err_code
}

my_function

echo $? # print 0

If the err_code exceed the 255, all the top bits will be dropped just like unsigned byte, how to explain this? Is there any feature document for this? I googled a lot w/o luck.

Thanks!

UPDATE:

Okay, I got it, it doesn't just happen only in shell but Unix-based system, the shell function is also called by command substitution. Thanks for linked questions!

Itachi
  • 5,777
  • 2
  • 37
  • 69
  • *it doesn't just happen only in shell but Unix-based system* - no, you misunderstand. The single byte limit is with *process* exit status on UNIX systems, correct. The return value of a *function* as only being a single byte is a feature of the shell. Most languages on UNIX (or elsewhere) will allow other object types to be returned from an internal function. – cdarke Jan 08 '19 at 09:07
  • @cdarke I see, python and javascript aren't included. – Itachi Jan 08 '19 at 09:54
  • Not just `python` and javascript, C, C++, perl, ruby, C#, Java, can all have functions/methods/subroutines that return more complex objects than just a byte. But none of them can exit their process with anything except an unsigned byte on UNIX. On Windows, by the way, processes can exit with a 32-bit int, which can lead to portability issues. – cdarke Jan 08 '19 at 14:08

1 Answers1

1

If you look at man bash and search for EXIT STATUS you will find the following explanation:

EXIT STATUS
   The  exit  status  of  an  executed command is the value returned by the waitpid system call or equivalent
   function.  Exit statuses fall between 0 and 255, though, as explained below,  the  shell  may  use  values
   above  125  specially.   Exit  statuses from shell builtins and compound commands are also limited to this
   range.  Under certain circumstances, the shell will use special values to indicate specific failure modes.

   For the shell's purposes, a command which exits with a zero exit status has succeeded.  An exit status  of
   zero  indicates  success.  A non-zero exit status indicates failure.  When a command terminates on a fatal
   signal N, bash uses the value of 128+N as the exit status.

   If a command is not found, the child process created to execute it returns a status of 127.  If a  command
   is found but is not executable, the return status is 126.

   If  a  command  fails because of an error during expansion or redirection, the exit status is greater than
   zero.

   Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error  occurs
   while  they  execute.   All  builtins  return  an  exit status of 2 to indicate incorrect usage, generally
   invalid options or missing arguments.

   Bash itself returns the exit status of the last command executed, unless a syntax error occurs,  in  which
   case it exits with a non-zero value.  See also the exit builtin command below.

If you really want to return values greater than 125, you can use echo instead of return like this:

#!/usr/bin/env bash
my_function () {
    echo 256
}
retval=$( my_function )
echo $retval
wef
  • 351
  • 3
  • 12