我正在尝试将脚本从 BASH 移植到 ASH (Almquist SHell),但遇到了间接引用问题。下面的函数
cmd() {
# first argument is the index to print (ie label)
arg=$1
# ditch the first argument
shift
# print the label (via indirect reference)
echo "${!arg}"
}
应该产生以下输出
cmd 1 one two three
one
cmd 2 one two three
two
cmd 3 one two three
three
这在 BASH 下按预期工作,但在 ASH(或 DASH)下运行时会生成“语法错误:错误替换”。这应该工作吗?如果不是,是否有使用间接引用的替代方法?
最佳答案
你可以试试eval
:
cmd() {
arg=$1
shift
eval "echo \$$arg"
}
关于unix - ASH 变量间接引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1014522/