UNIX, get environment variable

我是研究僧i 提交于 2019-11-30 07:51:11
Sergei Kurenkov

You can do:

printenv VARIABLE_NAME

type the following command in terminal, it will display all the list of environment variables

printenv

now print the wanted variable like this:

echo $VARIABLENAME

Do you mean something like this:

ENV() {
    printf 'echo $%s\n' $1 | sh
}

This works in plain old Bourne shell.

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');

Using ${!VAR_NAME} should be what you are looking for

> FOO=BAR123
> VAR_NAME=FOO
> echo ${VAR_NAME}
FOO
> echo ${!VAR_NAME}
BAR123

The solution really depends on what the restrictions are why you can't use a simple $VAR. Maybe you could call a shell that doesn't have the restrictions and let this sub-shell evaluate the variable:

bash -c 'echo $VAR'
( set -o posix ; set ) | grep $var

search for all unix-compatible format variables been used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!