idioms for returning multiple values in shell scripting

后端 未结 10 870
佛祖请我去吃肉
佛祖请我去吃肉 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:28

    Shell script functions can only return the exit status of last command executed or the exit status of that function specified explicitly by a return statement.

    To return some string one way may be this:

    function fun()
    {
      echo "a+b"
    }
    
    var=`fun` # Invoke the function in a new child shell and capture the results
    echo $var # use the stored result
    

    This may reduce your discomfort although it adds the overhead of creation of a new shell and hence would be marginally slower.

提交回复
热议问题