idioms for returning multiple values in shell scripting

后端 未结 10 910
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  旧时难觅i
    2020-12-29 21:29

    In the special case where your values never contain spaces, this read trick can be a simple solution:

    get_vars () {
      #...
      echo "value1" "value2"
    }
    
    read var1 var2 < <(get_vars)
    echo "var1='$var1', var2='$var2'"
    

    But of course, it breaks as soon as there is a space in one of the values. You could modify IFS and use a special separator in your function's echo, but then the result is not really simpler than the other suggested solutions.

提交回复
热议问题