Exit Code 128, What's the Reason?

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

2 Answers

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
0

This is specific to your shell variant. This or do not confirm your impression that 128 is some special exit code.

For exit 3.45 my versions of ksh and zsh return 3, tcsh returns 1 (does not actually exit), and ash returns 2 (but also does not actually exit).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest