【问题标题】:Using variables as part of variable name in unix [duplicate]在unix中使用变量作为变量名的一部分[重复]
【发布时间】:2014-10-31 18:15:44
【问题描述】:

我想将变量命名为a_${v}

例如:v 可以是 2013 2014

我现在向a_${v}声明一个变量

a_${v}=hI # a_2013 should be Hi

v=2014

所以a_${v}=Hello # a_2014 应该是你好

我尝试使用 eval 命令,虽然它在赋值时没有抛出错误,但我无法提取变量名的值

$ v=2013

$ eval a_${v}=Hi


$ v=2014

$ eval a_${v}=Hello

echo ${a_${v}}

不工作.. :(

我正在使用 bash,我不想更改变量名,即不想将值分配给另一个值

【问题讨论】:

  • @Pumbaa80 您引用的问题(及其答案)是bash 特定的,对这个问题有一个可移植的、非 bash 特定的答案会很有用。
  • 简单:删除最后一行,改为执行以下两行:varname=$(echo "a_${v}") 后跟 echo ${!varname}。不客气:D我会添加这个作为答案,但它不会让我:/
  • 这不是重复的!需要 /bin/sh 而不是 BASH 的答案,并且不使用 eval(如果可能)。没有一个链接的答案满足这一点。

标签: shell unix


【解决方案1】:

在 bash 中,您可以执行以下操作(注意最后一行中的感叹号语法):

#!/bin/bash

a_2014='hello 2014'
year=2014
varname=a_${year}
echo ${!varname}

【讨论】:

  • 这个小例子节省了很多时间 - 谢谢!!
【解决方案2】:

参数扩展不是递归的,因此文本${a_${v}}实际上是The contents of the variable whose name is 'a_${v}',并且shell抱怨这个变量名无效。

你可以使用eval命令实现递归扩展,如

eval printf '%s\n' "\${a_${v}}"

为了提高 shell 脚本的可读性和可维护性,您应该限制此类结构的使用并将它们包装在适当的结构中。有关示例,请参阅 FreeBSD 系统上提供的 rc.subr

【讨论】:

    【解决方案3】:

    在 bash 4.3 中:

    txt="Value of the variable"
    
    show() { echo "indirect access to $1: ${!1}"; }
    
    a_2014='value of a_2014'
    echo "$txt \$a_2014: $a_2014"
    show a_2014                    # <-------- this -----------------------+
                                   #                                       |
    prefix=a                       #                                       |
    year=2014                      #                                       |
    string="${prefix}_${year}"     #                                       |
    echo "\$string: $string"       #                                       |
    show "$string"            #$string contains a_2014 e.g. the same as ---+
    
    echo ===4.3====
    #declare -n  - only in bash 4.3
    #declare -n refvar=${prefix}_${year}
    #or
    declare -n refvar=${string}
    
    echo "$txt \$refvar: $refvar"
    show refvar
    
    echo "==assign to refvar=="
    refvar="another hello 2014"
    echo "$txt \$refvar: $refvar"
    echo "$txt \$a_2014: $a_2014"
    show a_2014
    show "$string" #same as above
    show refvar
    

    打印

    Value of the variable $a_2014: value of a_2014
    indirect access to a_2014: value of a_2014
    $string: a_2014
    indirect access to a_2014: value of a_2014
    ===4.3====
    Value of the variable $refvar: value of a_2014
    indirect access to refvar: value of a_2014
    ==assign to refvar==
    Value of the variable $refvar: another hello 2014
    Value of the variable $a_2014: another hello 2014
    indirect access to a_2014: another hello 2014
    indirect access to a_2014: another hello 2014
    indirect access to refvar: another hello 2014
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2020-09-22
      • 2015-03-23
      相关资源
      最近更新 更多