Bash:读入多个局部范围变量 [英] Bash: Read into multiple local scope variables
本文介绍了Bash:读入多个局部范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据此答案: https://stackoverflow.com/a/1952480/582917
我可以读入并因此分配多个变量.
I can read in and therefore assign multiple variables.
但是我希望这些变量在bash函数中是局部的,这样它才不会污染全局范围.
However I want those variables to be local to a bash function so it doesn't pollute the global scope.
有没有办法做类似的事情:
Is there a way to do something like:
func () {
local read a b <<< $(echo 123 435)
echo $a
}
func
echo $a
以上操作无效.读局部变量的好方法是什么?
The above doesn't work. What is a good way of reading into local variables?
推荐答案
您快到这里了:您只需要将变量定义为 local
,但是要预先在 read <中定义
声明:
You were almost there: you just have to define the variables as local
, but beforehand instead of in the read
declaration:
func () {
local a b
read a b <<< $(echo 123 435)
echo $a
}
测试
$ func
123
$ echo $a
$
这篇关于Bash:读入多个局部范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文