如何删除 cmd 变量?

发布于 2024-12-01 15:52:41 字数 112 浏览 0 评论 0原文

假设我在 CMD shell 中执行以下命令:

SET "FOO=bar"

除了回收 CMD shell 之外,还有其他方法可以取消定义此变量吗?

Let's say I execute the following in the CMD shell:

SET "FOO=bar"

Is there a way to undefine this variable, other than recycling the CMD shell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林迷了鹿 2024-12-08 15:52:41

是的,您可以使用以下命令取消设置

set FOO=

或显式使用:

set "FOO="

确保 = 符号后面没有尾随的无关(不可见)字符。即:

  • set FOO=set FOO=       不同。

Yes, you can unset it with

set FOO=

Or explicitly using:

set "FOO="

Ensure no trailing extraneous (invisible) characters come after the = sign. That is:

  • set FOO= is different from set FOO=      .
故人爱我别走 2024-12-08 15:52:41

取消设置变量的一种安全方法是同时使用引号,这样就不会出现尾随空格的问题。

set FOO=bar
echo %FOO%
set "FOO=" text after the last quote is ignored
echo %FOO%

A secure way to unset the variable is to use also quotes, then there aren't problems with trailing spaces.

set FOO=bar
echo %FOO%
set "FOO=" text after the last quote is ignored
echo %FOO%
如梦亦如幻 2024-12-08 15:52:41

另一种方法

@Echo oFF

setlocal
set FOO=bar
echo %FOO%
endlocal

echo %FOO%

pause

注意:这不适用于交互式命令提示符。但适用于批处理脚本。

another method

@Echo oFF

setlocal
set FOO=bar
echo %FOO%
endlocal

echo %FOO%

pause

Note: This would not work on an interactive command prompt. But works in batch script.

抠脚大汉 2024-12-08 15:52:41

这在我的 Windows 7 CMD shell 中适用于我:

set FOO=bar
echo %FOO% // bar
set FOO=
echo %FOO% // empty; calling "set" no longer lists it

This works for me in my Windows 7 CMD shell:

set FOO=bar
echo %FOO% // bar
set FOO=
echo %FOO% // empty; calling "set" no longer lists it
风为裳 2024-12-08 15:52:41

我只想提供以下内容作为评论,但我认为它本身就足够重要。

之前的很多答案都提到需要注意尾随空格;确实如此。然而,我发现有时尾随空格无论如何都想进入那里 - 特别是如果您正在执行命令行单行并且需要空格作为命令分隔符。

这是该问题的解决方案:

SET FOO=Bar
echo %FOO%
:: outputs Bar
SET "FOO="
echo %FOO%
:: outputs %FOO%

通过将声明用双引号引起来,可以完全避免间距问题。当通过连接来消除之间的空格(例如路径)来创建变量时,这也非常有用,例如:

SET A=c:\users\ && SET D=Daniel
SET P="%a%%d%"
ECHO %P%
:: outputs "C:\Users\ Daniel"
:: Notice the undesirable space there

I would offer the following only as a comment, but I think it's important enough to stand on its own.

A lot of the previous answers mentioned that one needs to beware of trailing spaces; and for sure that is true. However I've found that sometimes trailing spaces just want to get in there no matter what - particularly if you are doing a command line one-liner and need the space as a command separator.

This is the solution to that problem:

SET FOO=Bar
echo %FOO%
:: outputs Bar
SET "FOO="
echo %FOO%
:: outputs %FOO%

By wrapping the declaration in double quotes that way, the spacing issue can be avoided entirely. This can also really useful when variables are created by concatenating to eliminate spaces in between - for example - paths, e.g:

SET A=c:\users\ && SET D=Daniel
SET P="%a%%d%"
ECHO %P%
:: outputs "C:\Users\ Daniel"
:: Notice the undesirable space there
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文