UNIX,获取环境变量
由于一个荒谬的问题,我有一个荒谬的问题。
通常,如果我想在 UNIX shell 中获取环境变量的内容,我可以做
echo ${VAR}
让我们假设,由于我的荒谬情况,这是不可能的。
如何将环境变量的内容获取到标准输出,而无需查看命令本身(而不是输出)的人来查看环境变量的值?
我可以想象解决方案类似于 echo env(NAME_OF_VAR)
虽然我似乎找不到它。该解决方案必须在 sh 中工作。
PS 我无法为此编写脚本,它必须是内置的 Unix 命令(我知道,这是个可笑的问题)。
I have a ridiculous question due to a ridiculous problem.
Normally if I want to get the contents of an environment variable in a UNIX shell, I can do
echo ${VAR}
Let's assume, due to my ridiculous situation, that this isn't possible.
How do I get the contents of an environment variable to stdout, without someone who is looking at the command itself (not the output), see the value of the environment variable?
I can picture the solution being something like echo env(NAME_OF_VAR)
although I can't seem to find it. The solution has to work in sh.
PS I can't write a script for this, it must be a built-in Unix command (I know, ridiculous problem).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以这样做:
You can do:
在终端中键入以下命令,它将显示所有环境变量列表
printenv
现在打印所需的变量,如下所示:
echo $VARIABLENAME
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
使用
${!VAR_NAME}
应该是您正在寻找的Using
${!VAR_NAME}
should be what you are looking for这个怎么样:
How about this:
你的意思是这样的吗:
这适用于普通的旧 Bourne shell。
Do you mean something like this:
This works in plain old Bourne shell.
解决方案实际上取决于限制是什么,为什么您不能使用简单的
$VAR
。也许您可以调用一个没有限制的 shell,并让该子 shell 评估变量: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:搜索所有已使用的 UNIX 兼容格式变量
search for all unix-compatible format variables been used