bash 为单个命令取消设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18906350/
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
Unset an environment variable for a single command
提问by skeptical scientist
In Bash, we can set an environment variable for a single command this way:
在 Bash 中,我们可以通过这种方式为单个命令设置环境变量:
FOO=bar somecommand
What if we want to unseta variable for a single command?
如果我们想为单个命令取消设置变量怎么办?
采纳答案by JB.
Technically, they're not environment variables until someone export
s them. But you can at least set them to empty:
从技术上讲,除非有人为export
它们设置,否则它们不是环境变量。但是您至少可以将它们设置为空:
FOO= some command
If removing them from the environment is enough, you can use env
:
如果从环境中删除它们就足够了,您可以使用env
:
env -u FOO somecommand
回答by qwertzguy
env -u FOO somecommand
This will remove the environment variable FOO
from the somecommand
process' environment.
这将删除环境变量FOO
从somecommand
进程的环境。
And to unset multiple variables:
并取消设置多个变量:
env -u FOO -u FOO2 somecommand
回答by VonC
This is tricky when "somecommand
" is a shell function.
当 " somecommand
" 是一个shell 函数时,这很棘手。
One-shot environment variable assignments, such as 'FOO
' in "FOO=bar cmd
", exist only during the invocation of 'cmd
'.
However, if 'cmd
' happens to be a shell function, then 'FOO
' is assigned in the executing shell itself, and that assignment remains until the process exits (unless explicitly unset).
Since this side-effect of "FOO=bar shell_func
" is unlikely to be intentional, it should be avoided.
一次性环境变量赋值,例如“ FOO
”中的“ FOO=bar cmd
”,仅在调用“ ”期间存在cmd
。
但是,如果“ cmd
”恰好是一个 shell 函数,则FOO
在执行的 shell 本身中分配“ ”,并且该分配将一直保留到进程退出(除非明确取消设置)。
由于“ FOO=bar shell_func
”的这种副作用不太可能是故意的,因此应该避免。
To further illustrates how the FOO= aCommand
is dangerous, consider Git 2.26 (Q1 2020), which avoids "FOO= shell_function
(unsetting FOO
just for one command).
为了进一步说明这FOO= aCommand
是多么危险,请考虑 Git 2.26(2020 年第一季度),它避免了“FOO=” shell_function
(FOO
仅针对一个命令取消设置)。
See commit d6509da, commit a7fbf12, commit c7973f2(26 Dec 2019) by Jonathan Nieder (artagnon
).
(Merged by Junio C Hamano -- gitster
--in commit c7372c9, 30 Jan 2020)
请参阅Jonathan Nieder ( ) 的commit d6509da、commit a7fbf12、commit c7973f2(26 Dec 2019 )。(由Junio C Hamano合并-- --在提交 c7372c9 中,2020 年 1 月 30 日)artagnon
gitster
fetch test
: avoid use of "VAR= cmd" with a shell functionSigned-off-by: Jonathan Nieder
Just like assigning a nonempty value, assigning an empty value to a shell variable when calling a function produces non-portable behavior: in some shells, the assignment lasts for the duration of the function invocation, and in others, it persists after the function returns.
Use an explicit subshell with the
envvar
exported to make the behavior consistent across shells and crystal clear.All previous instances of this pattern used "
VAR=value
" (with nonemptyvalue
), which is already diagnosed automatically by "make test-lint" since a0a630192d(t/check-non-portable-shell: detect "FOO=bar shell_func
", 2018-07-13).
fetch test
: 避免在 shell 函数中使用 "VAR= cmd"签字人:乔纳森·尼德
就像分配非空值一样,在调用函数时将空值分配给 shell 变量会产生不可移植的行为:在某些 shell 中,赋值持续到函数调用的持续时间,而在另一些 shell 中,它在函数返回后持续存在.
将显式子shell 与
envvar
导出的内容一起使用,以使跨shell 的行为保持一致并且非常清晰。此模式的所有先前实例都使用“
VAR=value
”(非空value
),自a0a630192d(t/check-non-portable-shell:detect“FOO=bar shell_func
”,2018 年 7 月 13 日)以来,该模式已由“make test-lint”自动诊断。
For example, instead of:
例如,而不是:
GIT_TEST_PROTOCOL_VERSION= trace_fetch client origin to_fetch
Use a subshell:
使用子shell:
(
GIT_TEST_PROTOCOL_VERSION= &&
export GIT_TEST_PROTOCOL_VERSION &&
trace_fetch client origin to_fetch
) &&
...