【问题标题】:Bash expand variable in an another variable [duplicate]Bash在另一个变量中扩展变量[重复]
【发布时间】:2018-09-27 03:00:01
【问题描述】:

我有以下变量

my_country_code="green"
x="country"

echo ${my_$x_code}
bash: ${my_$x_code}: bad substitution

echo 应该打印绿色作为输出,但找不到任何可以提供正确输出的技术

【问题讨论】:

  • x=country; declare "my_${x}_code=green"; var="my_${x}_code"; echo "${!var}"

标签: bash shell scripting


【解决方案1】:
my_x_code="my_${x}_code"
echo ${!my_x_code}

【讨论】:

  • 请记住在How to Answer的“回答好问题”部分中的“...之前已经被问过并回答过很多次的问题”的注释
猜你喜欢
  • 2019-11-24
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 2011-02-20
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多