我对bash脚本感到困惑。

我有以下代码:

function grep_search() {
    magic_way_to_define_magic_variable_$1=`ls | tail -1`
    echo $magic_variable_$1
}

我希望能够创建一个变量名,其中包含命令的第一个参数,并承载例如ls的最后一行的值。

为了说明我想要的:

$ ls | tail -1
stack-overflow.txt

$ grep_search() open_box
stack-overflow.txt

那么,我应该如何定义/声明$magic_way_to_define_magic_variable_$1,我应该如何在脚本中调用它?

我已经试过eval, ${…}, \$${...},但我还是很困惑。


当前回答

将两个评分较高的答案结合成一个完整的例子,希望有用且不言自明:

#!/bin/bash

intro="You know what,"
pet1="cat"
pet2="chicken"
pet3="cow"
pet4="dog"
pet5="pig"

# Setting and reading dynamic variables
for i in {1..5}; do
        pet="pet$i"
        declare "sentence$i=$intro I have a pet ${!pet} at home"
done

# Just reading dynamic variables
for i in {1..5}; do
        sentence="sentence$i"
        echo "${!sentence}"
done

echo
echo "Again, but reading regular variables:"
echo $sentence1
echo $sentence2
echo $sentence3
echo $sentence4
echo $sentence5

输出:

你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

同样,但是读取的是常规变量: 你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

其他回答

对于索引数组,你可以像这样引用它们:

foo=(a b c)
bar=(d e f)

for arr_var in 'foo' 'bar'; do
    declare -a 'arr=("${'"$arr_var"'[@]}")'
    # do something with $arr
    echo "\$$arr_var contains:"
    for char in "${arr[@]}"; do
        echo "$char"
    done
done

关联数组可以类似地引用,但需要在declare上使用-A开关而不是-A。

将两个评分较高的答案结合成一个完整的例子,希望有用且不言自明:

#!/bin/bash

intro="You know what,"
pet1="cat"
pet2="chicken"
pet3="cow"
pet4="dog"
pet5="pig"

# Setting and reading dynamic variables
for i in {1..5}; do
        pet="pet$i"
        declare "sentence$i=$intro I have a pet ${!pet} at home"
done

# Just reading dynamic variables
for i in {1..5}; do
        sentence="sentence$i"
        echo "${!sentence}"
done

echo
echo "Again, but reading regular variables:"
echo $sentence1
echo $sentence2
echo $sentence3
echo $sentence4
echo $sentence5

输出:

你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

同样,但是读取的是常规变量: 你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

我最近一直在寻找更好的方法。联想数组对我来说太夸张了。看看我发现了什么

suffix=bzz
declare prefix_$suffix=mystr

...然后……

varname=prefix_$suffix
echo ${!varname}

从文档中可以看出:

The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. ... The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. ... If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion. The value is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion. ...

这应该可以工作:

function grep_search() {
    declare magic_variable_$1="$(ls | tail -1)"
    echo "$(tmpvar=magic_variable_$1 && echo ${!tmpvar})"
}
grep_search var  # calling grep_search with argument "var"

另外一个不依赖于您使用的shell/bash版本的方法是使用envsubst。例如:

newvar=$(echo '$magic_variable_'"${dynamic_part}" | envsubst)