【问题标题】:Combine two variables to form the identifier for another variable in Bash [duplicate]组合两个变量以形成 Bash 中另一个变量的标识符 [重复]
【发布时间】:2021-09-14 18:57:23
【问题描述】:

我希望能够获取两个变量的值并将它们连接在一起以形成 bash 脚本中另一个变量的标识符。

  final_answer="we did it"
  one="final"
  two="answer"
  t="${one}_${two}"
  echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"

不确定这是否可能,但似乎 bash 会以某种方式具有这种能力。

谢谢!

【问题讨论】:

  • 为什么这被标记为 bash zsh
  • 我想这两个答案都对我很好
  • @JLaF :由于 bash 和 zsh 的答案可能看起来非常不同,我建议您问两个单独的问题,每种语言一个。

标签: bash shell variables syntax zsh


【解决方案1】:
$ echo "${!t}"
we did it

详情请见http://mywiki.wooledge.org/BashFAQ/006#Indirection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 2021-11-08
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2014-10-03
    相关资源
    最近更新 更多