Search code examples
linuxshopenwrt

command return in shell script


I have developed a script shell in OpenWrt as below, I noticed a strange behavior (the "return") as shown in my comments at the end of script. It returns 43 instead of 9003 and 45 instead of 9005, do you have any explanation for this behavior?

#!/bin/sh

get_value() {
case "$1" in
    aa)
    if [ "$2" = "1" ]; then
        return 9003
    fi
# function fn1 to execute
    fn1
    return 0
    ;;
    bb)
    if [ "$2" = "1" ]; then
        return 9003
    fi
# function fn2 to execute
    fn2
    return 0
    ;;
    cc)
    if [ "$2" = "1" ]; then
        return 9003
    fi
# function fn3 to execute
    fn3
    return 0
    ;;
esac
return 9005
}
# when a call get_value
get_value aa 0
echo $?
# result 0
get_value aa 1
echo $?
# result 43 (not 9003)
get_value dd 1
echo $?
# result 45 (not 9005)

Solution

  • On most Unix systems, return values are interpreted as modulo 256. Since 9003 mod 256 is 43, and 9005 mod 256 equals 45, you get these return values, respectively.