获取环境变量的值,在UNIX [英] Getting value of environment variable in unix

查看:126
本文介绍了获取环境变量的值,在UNIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

mydata.properties:

VAR1=Data1
VAR2=Data2

现在我要定义一个名为VAR3新的变量,可容纳两种或VAR1像VAR2:

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

mydata.properties:

VAR1=Data1
VAR2=Data2
VAR3=VAR2

为了使这些变量来提供给我使用这个命令我bash命令

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

source mydata.properties

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

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

Value of VAR3 is Data2

我尝试了不同的选项,如

I tried different options like

回声VAR3的值是$$ VAR3

这给我的垃圾输出。

回声VAR3的值是$ {$} VAR3

这给我误差的$ {$} VAR3价值:替代不好

请帮我如何得到这个输出。

Please help me how to get this output.

推荐答案

如果你真的需要展开VAR3点(而不是仅仅设置VAR3到VAR2的价值开始)的变量,你可以使用间接变量扩张与 $ {VARNAME!}

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}:

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

这篇关于获取环境变量的值,在UNIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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