test_function()
{
var=$1
echo "**$var**"
var2="bb 6"
return $var2
}
return_val=`test_function "aa 3" `
echo $return_val
我需要将非数字字符传递给函数并从函数返回非数字值。 上面的代码失败了。请帮忙。
答案 0 :(得分:3)
试试这个
function test_function()
{
var=$1
echo "**$var**"
var2="bb 6"
echo $var2
}
return_val=$( test_function "aa 3" )
echo $return_val
答案 1 :(得分:1)
您可以使用eval
命令将值返回到参数output
test_function()
{
input=$1;
echo "**$input**"
var_out="bb 6"
eval "$2=\"$var_out\""
}
test_function "This is input" output
echo $output;
答案 2 :(得分:0)
return用于返回函数状态代码,它必须后跟一个整数 调用函数后,你可以使用$?获取状态代码
$ help return
return: return [n]
Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last command executed within the function or script.
Exit Status:
Returns N, or failure if the shell is not executing a function or script.
如果您想获取var2的值,则应使用echo $var2
代替return $var2