bash 如何打印/回显环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40050793/
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
How to print / echo environment variables?
提问by ThomasReggi
How do I print the environment variable just being set?
如何打印刚刚设置的环境变量?
NAME=sam echo "$NAME" # empty
You can see here using eval
it works. Is this the way?
你可以在这里看到使用eval
它的工作原理。这是方法吗?
NAME=sam eval 'echo $NAME' # => sam
回答by heemayl
These need to go as different commands e.g.:
这些需要作为不同的命令,例如:
NAME=sam; echo "$NAME"
NAME=sam && echo "$NAME"
The expansion $NAME
to empty string is done by the shell earlier, before running echo
, so at the time the NAME
variable is passed to the echo
command's environment, the expansion is already done (to null string).
扩展$NAME
为空字符串是由 shell 在运行之前完成的echo
,因此在将NAME
变量传递给echo
命令的环境时,扩展已经完成(到空字符串)。
To get the same result in one command:
要在一个命令中获得相同的结果:
NAME=sam printenv NAME
回答by mklement0
To bring the existing answers together with an important clarification:
将现有答案与重要说明结合在一起:
As stated, the problem with NAME=sam echo "$NAME"
is that $NAME
gets expanded by the current shell beforeassignment NAME=sam
takes effect.
如前所述,问题NAME=sam echo "$NAME"
在于在分配生效之前$NAME
被当前 shell 扩展。NAME=sam
Solutions that preserve the original semantics(of the (ineffective) solution attempt NAME=sam echo "$NAME"
):
保留原始语义的解决方案((无效的)解决方案尝试的NAME=sam echo "$NAME"
):
Use either eval
[1](as in the question itself), or printenv
(as added by Aaron McDaid to heemayl's answer), or bash -c
(from Ljm Dullaart's answer), in descending order of efficiency:
使用eval
[1](如问题本身),或printenv
(由 Aaron McDaid 添加到heemayl 的回答),或bash -c
(来自Ljm Dullaart 的回答),按效率降序排列:
NAME=sam eval 'echo "$NAME"' # use `eval` only if you fully control the command string
NAME=sam printenv NAME
NAME=sam bash -c 'echo "$NAME"'
printenv
is not a POSIX utility, but it is available on both Linux and macOS/BSD.
printenv
不是 POSIX 实用程序,但它在 Linux 和 macOS/BSD 上都可用。
What this style of invocation (<var>=<name> cmd ...
) does is to define NAME
:
这种调用方式 ( <var>=<name> cmd ...
) 的作用是定义NAME
:
- as an environmentvariable
- that is only defined for the command being invoked.
- 作为环境变量
- 即只对被调用的命令定义。
In other words: NAME
only exists for the command being invoked, and has no effect on the current shell(if no variable named NAME
existed before, there will be none after; a preexisting NAME
variable remains unchanged).
换句话说:NAME
只存在于被调用的命令中,对当前shell没有影响(如果NAME
之前不存在named变量,则之后也不存在;先前存在的NAME
变量保持不变)。
POSIX defines the rules for this kind of invocation in its Command Search and Executionchapter.
POSIX 在其命令搜索和执行章节中定义了这种调用的规则。
The following solutions work very differently(from heemayl's answer):
以下解决方案的工作方式非常不同(来自heemayl 的回答):
NAME=sam; echo "$NAME"
NAME=sam && echo "$NAME"
While they produce the same output, they instead define:
虽然它们产生相同的输出,但它们定义了:
- a shellvariable
NAME
(only) rather than an environmentvariable- if
echo
were a command that relied on environmentvariableNAME
, it wouldn't be defined (or potentially defined differently from earlier).
- if
- that lives onafter the command.
- 一个壳变量
NAME
(只),而不是一个环境变量- 如果
echo
是依赖于环境变量的命令,NAME
它将不会被定义(或可能与之前的定义不同)。
- 如果
- 是住在该命令后。
Note that every environment variable is also exposed as a shell variable, but the inverse is not true: shell variables are only visible to the current shell and its subshells, but notto child processes, such as external utilities and (non-sourced) scripts (unless they're marked as environment variables with export
or declare -x
).
请注意,每个环境变量也作为 shell 变量公开,但反之则不然:shell 变量仅对当前 shell 及其子 shell 可见,而对子进程(例如外部实用程序和(非源)脚本)不可见(除非它们用export
或标记为环境变量declare -x
)。
[1] Technically, bash
is in violation of POSIX here (as is zsh
): Since eval
is a specialshell built-in, the preceding NAME=sam
assignment should cause the the variable $NAME
to remain in scope after the command finishes, but that's not what happens.
However, when you run bash
in POSIX compatibility mode, it iscompliant.dash
and ksh
are always compliant.
The exact rules are complicated, and some aspects are left up to the implementations to decide; again, see Command Search and Execution.
Also, the usual disclaimer applies: Use eval
only on input you fully control or implicitly trust.
[1] 从技术上讲,bash
这里违反了 POSIX(原样zsh
):由于eval
是一个特殊的内置 shell,前面的NAME=sam
赋值应该导致$NAME
命令完成后变量保持在作用域内,但事实并非如此。
但是,当您bash
在 POSIX 兼容模式下运行时,它是兼容的。dash
并且ksh
始终合规。
确切的规则很复杂,有些方面由实现来决定;再次,请参阅命令搜索和执行。
此外,通常的免责声明适用:仅在您完全控制或隐含信任的输入上使用eval
。
回答by ThomasReggi
This works too, with the semi-colon.
这也适用于分号。
NAME=sam; echo $NAME
NAME=sam; echo $NAME
回答by Ljm Dullaart
The syntax
语法
variable=value command
is often used to set an environment variables for a specific process. However, you must understand which process gets what variable and who interprets it. As an example, using two shells:
通常用于为特定进程设置环境变量。但是,您必须了解哪个过程获得了哪些变量以及由谁来解释它。例如,使用两个外壳:
a=5
# variable expansion by the current shell:
a=3 bash -c "echo $a"
# variable expansion by the second shell:
a=3 bash -c 'echo $a'
The result will be 5 for the first echo and 3 for the second.
第一个回声的结果为 5,第二个为 3。