Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash method return value is always modulo 256

Tags:

linux

bash

shell

I have a bash script method that returns the input value as such. The return value however, is always the value modulo 256. I googled for a while and found this article that said it's always modulo 256 (something I'd already guessed), but didn't explain why. Can someone shed some light on why it does this?

var=$1
hello () {
    return $var
}
hello

ret=$?
echo $ret

>>> ./script 300
>>> 44
like image 420
th3an0maly Avatar asked Sep 18 '25 17:09

th3an0maly


1 Answers

The return value is limited to 1 byte (8 bits). Taking the least significant byte from an integer is equivalent to modulo 256 (2^8).

like image 197
Coeffect Avatar answered Sep 20 '25 08:09

Coeffect