使用用户定义的名称创建环境变量-间接变量扩展 [英] creating environment variable with user-defined name - indirect variable expansion

查看:60
本文介绍了使用用户定义的名称创建环境变量-间接变量扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在bash脚本中创建一个环境变量,用户将输入要创建的环境变量的名称,并输入其值.

I am trying to create an environment variable in bash script, user will input the name of environment variable to be created and will input its value as well.

这是一种硬编码的方式,用于阐述我的问题:

this is a hard coded way just to elaborate my question :

#!/bin/bash
echo Hello 
export varName="nameX" #
echo $varName 
export "$varName"="val" #here I am trying to create an environment 
#variable  whose name is nameX and assigning it value val
echo $nameX 

工作正常它的输出是:

Hello
nameX
val

但是,我想要一个通用代码.因此,我试图从用户那里获取变量名称及其值的输入,但是我遇到了麻烦.我不知道如何回显用户定义的变量

But, I want a generic code. So I am trying to take input from user the name of variable and its value but I am having trouble in it. I don't know how to echo variable whose name is user-defined

echo "enter the environment variable name"
read varName
echo "enter the value to be assigned to env variable"
read value
export "$varName"=$value

现在,我不知道如何回显环境变量如果我喜欢这样:

Now, I don't know how to echo environment variable if I do like this :

echo "$varName"

它输出用户为环境变量指定的名称,而不是为其分配的值.如何回显其中的值?

it outputs the name that user has given to environment variable not the value that is assigned to it. how to echo value in it?

谢谢

推荐答案

要获取关闭:OP的问题可以归结为:

To get closure: the OP's question boils down to this:

如何获取其名称存储在bash中的另一个变量中的变量的值?

How can I get the value of a variable whose name is stored in another variable in bash?

var='value'    # the target variable
varName='var'  # the variable storing $var's *name*

gniourf_gniourf在评论中提供了解决方案:

gniourf_gniourf provided the solution in a comment:

使用bash的间接扩展功能:

echo "${!varName}"  # -> 'value'

varname 之前的告诉bash不要返回 $ varName 的值,而是返回其 name的变量的值 $ varName 的值.
与直接变量引用(通常)不同,需要使用大括号( {} ).
参见 https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

The ! preceding varName tells bash not to return the value of $varName, but the value of the variable whose name is the value of $varName.
The enclosing curly braces ({ and }) are required, unlike with direct variable references (typically).
See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

上面的页面还描述了 $ {!prefix @} $ {!prefix *} 形式,它们返回变量的列表以前缀开头的名称.

The page above also describes the forms ${!prefix@} and ${!prefix*}, which return a list of variable names that start with prefix.

bash 4.3 + 支持更灵活机制: namerefs ,通过 declare -n 或内部函数中的 local -n :

bash 4.3+ supports a more flexible mechanism: namerefs, via declare -n or, inside functions, local -n:

注意:对于当前的特定用例,间接扩展是更简单的解决方案.

var='value'

declare -n varAlias='var'  # $varAlias is now another name for $var

echo "$varAlias" # -> 'value' - same as $var

此方法的优点是nameref实际上是原始变量的另一个名称(存储位置),因此您还可以分配 nameref 来更新原始变量:

The advantage of this approach is that the nameref is effectively just an another name for the original variable (storage location), so you can also assign to the nameref to update the original variable:

varAlias='new value'  # assign a new value to the nameref

echo "$var" # -> 'new value' - the original variable has been updated

请参见 https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html

兼容性说明:

  • 间接扩展和名称引用不符合POSIX;严格符合POSIX的外壳将没有任何功能.
  • ksh zsh 具有可比的功能,但是语法不同.
  • Indirect expansion and namerefs are NOT POSIX-compliant; a strictly POSIX-compliant shell will have neither feature.
  • ksh and zsh have comparable features, but with different syntax.

这篇关于使用用户定义的名称创建环境变量-间接变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆