简体   繁体   English

如何使用 bash 将带有参数的 function 的 output 分配给变量?

[英]How can I assign the output of a function with parameters to a variable using bash?

example (){
......
local var
......
return $var 
}

user_val=$(example $number1 $number2) 

I need user_val and i will it.我需要 user_val,我会的。 I got error.我得到了错误。 Code can't detect the function when I try it this way.当我以这种方式尝试时,代码无法检测到 function。

@Barmar is correct. @Barmar 是正确的。 Replace return with echo in your function. Using var=$(...) assigns the output of the function to the variable.在 function 中将return替换为echo 。使用var=$(...)function的 output 分配给变量。

When using return the value is saved in $?使用return时,值保存在$? . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM