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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:13:03  来源:igfitidea点击:

Unset an environment variable for a single command

bashunixenvironment-variables

提问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 exports 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 FOOfrom the somecommandprocess' environment.

这将删除环境变量FOOsomecommand进程的环境。

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= aCommandis dangerous, consider Git 2.26 (Q1 2020), which avoids "FOO= shell_function(unsetting FOOjust for one command).

为了进一步说明这FOO= aCommand是多么危险,请考虑 Git 2.26(2020 年第一季度),它避免了“FOO=” shell_functionFOO仅针对一个命令取消设置)。

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 d6509dacommit a7fbf12commit c7973f2(26 Dec 2019 )(由Junio C Hamano合并-- --提交 c7372c9 中,2020 年 1 月 30 日)artagnon
gitster

fetch test: avoid use of "VAR= cmd" with a shell function

Signed-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 envvarexported to make the behavior consistent across shells and crystal clear.

All previous instances of this pattern used "VAR=value" (with nonempty value), 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
) &&
...