bash 如何在bash中使用变量的值作为另一个变量的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9714902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:47:02  来源:igfitidea点击:

How to use a variable's value as another variable's name in bash

bash

提问by Haiyuan Zhang

I want to declare a variable, the name of which comes from the value of another variable, and I wrote the following piece of code:

我想声明一个变量,它的名字来自另一个变量的值,我写了下面一段代码:

a="bbb"
$a="ccc"

but it didn't work. What's the right way to get this job done?

但它没有用。完成这项工作的正确方法是什么?

回答by Kaz

evalis used for this, but if you do it naively, there are going to be nasty escaping issues. This sort of thing is generally safe:

eval用于此目的,但如果您天真地这样做,将会出现令人讨厌的逃逸问题。这种事情通常是安全的:

name_of_variable=abc

eval $name_of_variable="simpleword"   # abc set to simpleword

This breaks:

这打破了:

eval $name_of_variable="word splitting occurs"

The fix:

修复:

eval $name_of_variable="\"word splitting occurs\""  # not anymore

The ultimate fix: put the text you want to assign into a variable. Let's call it safevariable. Then you can do this:

最终解决方法:将您要分配的文本放入变量中。让我们称之为safevariable。然后你可以这样做:

eval $name_of_variable=$safevariable  # note escaped dollar sign

Escaping the dollar sign solves all escape issues. The dollar sign survives verbatim into the evalfunction, which will effectively perform this:

转义美元符号可以解决所有转义问题。美元符号逐字保留在eval函数中,它将有效地执行此操作:

eval 'abc=$safevariable' # dollar sign now comes to life inside eval!

And of course this assignment is immune to everything. safevariablecan contain *, spaces, $, etc. (The caveat being that we're assuming name_of_variablecontains nothing but a valid variable name, and one we are free to use: not something special.)

当然,这项任务不受任何影响。safevariable可以包含*, 空格,$等(需要注意的是,我们假设只name_of_variable包含一个有效的变量名,并且我们可以自由使用:不是什么特别的东西。)

回答by Flimm

You can use declareand !, like this:

您可以使用declareand !,如下所示:

John="nice guy"
programmer=John
echo ${!programmer} # echos nice guy

Second example:

第二个例子:

programmer=Ines
declare $programmer="nice gal"
echo $Ines # echos nice gal

回答by potong

This might work for you:

这可能对你有用:

foo=bar
declare $foo=baz
echo $bar
baz

or this:

或这个:

foo=bar
read $foo <<<"baz"
echo $bar
baz

回答by another.anon.coward

You could make use of evalfor this.
Example:

你可以利用eval这个。
例子:

$ a="bbb"
$ eval $a="ccc"
$ echo $bbb
ccc

Hope this helps!

希望这可以帮助!

回答by bat_ventzi

If you want to get the value of the variable instead of setting it you can do this

如果你想获取变量的值而不是设置它,你可以这样做

var_name1="var_name2"
var_name2=value_you_want
eval temp_var=$$var_name1
echo "$temp_var"

You can read about it here indirect references.

您可以在此处阅读间接引用

回答by Josh B

You can assign a value to a variable using simple assignment using a value from another variable like so:

您可以使用来自另一个变量的值使用简单的赋值来为变量赋值,如下所示:

#!/usr/bin/bash

#variable one
a="one"

echo "Variable a is $a"
#variable two with a's variable
b="$a"

echo "Variable b is $b"

#change a
a="two"
echo "Variable a is $a"
echo "Variable b is $b"

The output of that is this:

输出是这样的:

Variable a is one
Variable b is one
Variable a is two
Variable b is one

So just be sure to assign it like this b="$a" and you should be good.

所以一定要像这样分配它 b="$a" ,你应该很好。