bash 在unix中获取环境变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17550793/
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-18 05:52:35  来源:igfitidea点击:

Getting value of environment variable in unix

bashunix

提问by Chaitanya

I have a properties file to set some environment variables say:

我有一个属性文件来设置一些环境变量说:

mydata.properties:

VAR1=Data1
VAR2=Data2

Now I want to define a new variable called VAR3 that can hold either VAR1 or VAR2 like:

现在我想定义一个名为 VAR3 的新变量,它可以包含 VAR1 或 VAR2,例如:

mydata.properties:

VAR1=Data1
VAR2=Data2
VAR3=VAR2

To make these variables to be available to my bash session I am using this command

为了使这些变量可用于我的 bash 会话,我正在使用此命令

source mydata.properties

Now my requirement is to print the value for VAR3 so that it can print the internal data of the sourced VAR2 variable like:

现在我的要求是打印 VAR3 的值,以便它可以打印源 VAR2 变量的内部数据,如:

Value of VAR3 is Data2

I tried different options like

我尝试了不同的选择,比如

echo "Value of VAR3 is $$VAR3"

echo "Value of VAR3 is $$VAR3"

This gives me junk output.

这给了我垃圾输出。

or echo "Value of VAR3 is ${$VAR3}"

或者 echo "Value of VAR3 is ${$VAR3}"

This give me error as Value of ${$VAR3}: bad substitution

这给了我错误 Value of ${$VAR3}: bad substitution

Please help me how to get this output.

请帮助我如何获得此输出。

回答by Gordon Davisson

If you really need to expand the variable that VAR3 points to (instead of just setting VAR3 to the value of VAR2 to begin with), you can use indirect variable expansion with ${!varname}:

如果你真的需要扩展 VAR3 指向的变量(而不是仅仅将 VAR3 设置为 VAR2 的值),你可以使用间接变量扩展${!varname}

$ VAR1=Data1
$ VAR2=Data2
$ VAR3=VAR2
$ echo "${!VAR3}"
Data2

回答by tike

I don't use bash, but

我不使用 bash,但是

mydata.properties:

VAR1=Data1
VAR2=Data2
VAR3=$VAR2

should do it. Note the extra $ infront of var2 in the last line.

应该这样做。请注意最后一行中 var2 前面的额外 $。

回答by DevSolar

You might want to get into the habit of doing it this way:

您可能想养成这样做的习惯:

VAR3=${VAR2}

This is the same as VAR3=$VAR2, but when you are using variables embedded in other text (like VAR3=${VAR2}_foo), you will need the {}, so it's a good idea to use them by default.

这与 相同VAR3=$VAR2,但是当您使用嵌入其他文本(如VAR3=${VAR2}_foo)的变量时,您将需要{},因此默认情况下使用它们是个好主意。

回答by David W.

I think you want to know how to make a reference in BASH. The syntax can be rather tricky. It involves using evaland echoto be able to set the value:

我想您想知道如何在 BASH 中进行引用。语法可能相当棘手。它涉及使用evalecho能够设置值:

VAR2=Data2
VAR3=VAR2   #Reference!

echo "The value of $VAR3 is $(eval echo $$VAR3)"

It's better to use hash arrays instead of trying to use references directly:

最好使用哈希数组而不是尝试直接使用引用:

REF[VAR3]=$VAR2

echo "The value of VAR3 = ${REF[VAR3]}"

Or, as others point out, you could have simply done this:

或者,正如其他人指出的那样,您可以简单地这样做:

VAR3="$VAR2"

if you merely want to set $VAR3to be the same as $VAR2, and you don't care about references.

如果您只想设置$VAR3为与 相同$VAR2,并且您不关心引用。

回答by Nathan Wallace

You want

你要

VAR2=$VAR3

then

然后

echo "Value of VAR3 is $VAR3"

Note the single $in both places. You use the $to reference the value of variables, so you need to use it when assigning the value of VAR2, and when printing its value in the echo command

注意$两个地方的单曲。您使用$来引用变量的值,因此在分配 VAR2 的值时以及在 echo 命令中打印其值时需要使用它