I have variables that look something like this:
我有这样的变量
$INFOMonday
$INFOTuesday
In my Bash script, I would like to use the value of todays variable, so to say. I extracted the day with:
在我的Bash脚本中,我想使用todays变量的值。我用:
dayofweek=$(date --date=${dateinfile#?_} "+%A")
What I need help with, is doing the following:
我需要帮助的是:
echo $INFO$dayofweek
Is there any way of adding a variable as part of a variables name? For example, if it's monday, the echo would return the value of $INFOMonday.
是否有办法将变量作为变量名的一部分添加?例如,如果是周一,echo将返回INFOMonday的价值。
1 个解决方案
#1
2
The old-style way of doing this is with the indirection operator ${!variable}
:
老式的方法是使用间接操作符${!variable}:
dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}
Note that in ${!var}
, var
must be a variable-name, not a string expression or other type of substitution. It's a little clunky.
注意,在$ { !var}, var必须是一个变量名,而不是字符串表达式或其他类型的替换。有点笨重。
The newer way of doing it would be with an associative array.
更新的方法是使用关联数组。
#1
2
The old-style way of doing this is with the indirection operator ${!variable}
:
老式的方法是使用间接操作符${!variable}:
dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}
Note that in ${!var}
, var
must be a variable-name, not a string expression or other type of substitution. It's a little clunky.
注意,在$ { !var}, var必须是一个变量名,而不是字符串表达式或其他类型的替换。有点笨重。
The newer way of doing it would be with an associative array.
更新的方法是使用关联数组。