The operating system exit codes are unsigned 8-bit integers, so the only valid exit codes are 0..255.
The reason you get 254 is because the lower 8 bits of int -2 are treated as an unsigned number.
$ for q in -2 -1 0 1 2 253 254 255 256 257 ; do perl -e'exit $ARGV[0]' -- $q printf '%3d => %3d\n' $q $? done -2 => 254 -1 => 255 0 => 0 1 => 1 2 => 2 253 => 253 254 => 254 255 => 255 256 => 0 257 => 1
source share