Linux – exit code 128, what’s the reason

bashexitexit codelinux

As per definition exit code 128 means 'invalid exit argument'.
But i always get 255 (Exit status out of range) in case argument is invalid like float number.

Is this the proprietary implementation on my linux distribution?

# exit 1.234
exit
bash: exit: 1.234: numeric argument required

$ echo $?
255   //this should be 128?

# exit -1
exit

$ echo $?
255   //this is okay

Best Answer

There is nothing within Bash documentation that says 128 is the required invalid exit code.

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.

The last command is the bash builtin exit (from man page)

exit [n]

Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed.

Checked specification for WEXITSTATUS.

WEXITSTATUS(stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

So exit is restricted to an 8 bit integer ( 0 - 255 ), so -1 would be 255. Exit only understands an integer argument and not floats, so it's likely kicking out a default -1.

bash$ echo $BASH_VERSION
4.1.10(4)-release

bash$ exit foo
exit
bash: exit: foo: numeric argument required
$ echo $?    
255

bash$ exit 2
exit
$ echo $?
2

bash$ exit -2
exit
$ echo $?
254
Related Question