有返回值

控制流 / 跳转语句 / return 语句

#return gives only an exit code from 0 to 255, 0 means true:
#the function is a condition itself
containNumber() {
    local number=$1
    shift
    local current
    for current in "$@"; do
        if (( current == number )); then
            return 0
        fi
    done
    return 1
}

data=(1 2 3)
containNumber 2 "${data[@]}" && isContain2=true || isContain2=false
#isContain2 is true

containNumber 4 "${data[@]}" && isContain4=true || isContain4=false
#isContain4 is false

#any other value goes out by echo and is taken by $( )
indexOf() {
    local number=$1
    shift
    local values=("$@") i
    for i in "${!values[@]}"; do
        if (( values[i] == number )); then
            echo "$i"
            return
        fi
    done
    echo "-1"
}

index3=$(indexOf 3 "${data[@]}")
#index3 is 2

echo "isContain2 is $isContain2"
echo "isContain4 is $isContain4"
echo "index3 is $index3"