Returning Values from Bash Functions
Bash functions, unlike functions in most programming languages do not allow you to return a value to the caller.
When a bash function ends its return value is its status: zero for success, non-zero for failure.To return values, you
can set a global variable with the result, or use command substitution, or you can pass in the name of a variable
to use as the result variable. The examples below describe these different mechanisms.
Although bash has a return statement, the only thing you can specify with it is the function's status, which
is a numeric value like the value specified in an exit statement. The status value is stored in the $? variable. If a
function does not contain a return statement, its status is set based on the status of the last statement executed in the
function. To actually return arbitrary values to the caller you must use other mechanisms.
The simplest way to return a value from a bash function is to just set a global variable to the result. Since all variab
-les in bash are global by default this is easy:
function myfunc() { myresult='some value' } myfunc echo $myresult
The code above sets the global variable myresult to the function result. Reasonably simple, but as we all know,
using global variables, particularly in large programs, can lead to difficult to find bugs.
A better approach is to use local variables in your functions. The problem then becomes how do you get the result to
the caller. One mechanism is to use command substitution:
function myfunc() { local myresult='some value' echo "$myresult" } result=$(myfunc)
echo $result
Here the result is output to the stdout and the caller uses command substitution to capture the value in a
variable. The variable can then be used as needed.
说明:
上面的两种方法都不够好,第一种方法的缺点文中已经提到:全局变量的使用会使程序的错误调试非常麻烦。而第二种方
法的缺点文中没有提到“If you're calling a function thousands of times, the fork method "myvar=$(myfunc)" will be much
slower.”。所以推荐给大家的是第三种方法:
The other way to return a value is to write your function so that it accepts a variable name as part of its comm
-and line and then set that variable to the result of the function:
function myfunc() { local __resultvar=$1 local myresult='some value' eval $__resultvar="'$myresult'" } myfunc result echo $result
Since we have the name of the variable to set stored in a variable, we can't set the variable directly, we have to
use eval to actually do the setting. The eval statement basically tells bash to interpret the line twice, the first
interpretation above results in the string result='some value' which is then interpreted once more and ends
up setting the caller's variable.
When you store the name of the variable passed on the command line, make sure you store it in a local variable
with a name that won't be (unlikely to be) used by the caller (which is why I used __resultvar rather than just resultvar).
If you don't, and the caller happens to choose the same name for their result variable as you use for storing the name, the
result variable will not get set. For example, the following does not work:
function myfunc() { local result=$1 local myresult='some value' eval $result="'$myresult'" } myfunc result echo $result
The reason it doesn't work is because when eval does the second interpretation and evaluates result='some value',
result is now a local variable in the function, and so it gets set rather than setting the caller's result variable.
For more flexibility, you may want to write your functions so that they combine both result variables and command
substitution:
function myfunc() { local __resultvar=$1 local myresult='some value' if [[ "$__resultvar" ]]; then eval $__resultvar="'$myresult'" else echo "$myresult" fi } myfunc result echo $result result2=$(myfunc) echo $result2
Here, if no variable name is passed to the function, the value is output to the standard output.
说明:
我个人认为没有必要引入局部变量__resultvar, 所以我使用下面的代码:
function myfunc() { local myresult='some value' eval "$1"="'$myresult'" } myfunc result echo $result
如果大家对这种方法可能存在的问题有自己的观点的话,欢迎大家与我交流。
最后附上代码:
#!/bin/bash #File: test.sh #Author: lxw #Time: 2014-12-21 func0() { #result0 is a global variable. #All variables in bash are global by default. result0="value 0" } func1() { result1="value 1" #NOTE:This echo result will not show directly. echo "$result1" } func2() { local __resultVar=$1 local result2="value 2" #NOTE: "'$result2'": ' is used in case that there are spaces in result2. eval $__resultVar="'$result2'" } func3() { local result3="value 3" eval "$1"="'$result3'" } echo "result0: $result0" #nothing func0 echo "result0: $result0" #value 0 echo "result1: $result1" #nothing #NOTE: func1 runs in a child shell. res1=$(func1) echo "result1: $result1" #nothing #Note. echo "res1: $res1" #value 1 echo "res2: $res2" #nothing func2 res2 echo "res2: $res2" #value 2 echo "res3: $res3" #nothing func3 res3 echo "res3: $res3" #value 3
输出如下:
lxw@16:23:55:~$ bash test.sh result0: result0: value 0 result1: result1: res1: value 1 res2: res2: value 2 res3: res3: value 3 lxw@16:23:57:~$
摘录自:
Returning Values from Bash Functions: http://www.linuxjournal.com/content/return-values-bash-functions