idioms for returning multiple values in shell scripting

后端 未结 10 907
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 20:56

Are there any idioms for returning multiple values from a bash function within a script?

http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multipl

10条回答
  •  长发绾君心
    2020-12-29 21:49

    you can make use of associative arrays with you have bash 4 eg

    declare -A ARR
    function foo(){
      ...
      ARR["foo_return_value_1"]="VAR1"
      ARR["foo_return_value_2"]="VAR2"
    }
    

    you can combine them as strings.

    function foo(){
      ...
      echo "$var1|$var2|$var3"
    }
    

    then whenever you need to use those return values,

    ret="$(foo)"
    IFS="|"
    set -- $ret
    echo "var1 one is: $1"
    echo "var2 one is: $2"
    echo "var3 one is: $3"
    

提交回复
热议问题