如何抑制DOS和Windows CMD中的环境变量扩展? [英] How do you suppress environment variable expansion within DOS and Windows CMD?

查看:129
本文介绍了如何抑制DOS和Windows CMD中的环境变量扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简化的示例,其中包含我要执行的修改的变量名称.同样为了简单起见,我显示的是命令行版本而不是bat文件版本.

This is a simplified example with modified variable names of what I want to do. Also for simplicity sake, I am showing the command line version rather than the bat file version.

我正在做以下事情.

> echo %foo%

%foo%

但是,如果foo是有效的环境变量,由于环境变量的扩展,我无法获得所需的输出(%foo%).

However, if foo is a valid environment variable, I do not get desired output (%foo%) due to environment variable expansion.

> set foo=bar
> echo %foo%
> echo %%foo%%

bar
%bar%

现在,我有一个技巧可以做(以下示例),但是我想知道是否存在一种更干净的方法来输出%字符或抑制环境变量扩展.

Now, I have a hack to do (following example) this but I was wondering if there is a cleaner way to either output a % character or to suppress environment variable expansion.

> set foo=bar
> set percent=%
> echo %percent%foo%percent%

%foo%

此外,如果bat文件中所需的解决方案有所不同(例如%%而不是%%1%而不是%1),请告诉我.

Also, if the required solution is different in a bat file (like %% rather than % or %1% rather than %1) please let me know.

我的实际用例是在带有SETX的bat文件中,以设置依赖于另一个环境变量的全局环境变量在其中进行扩展,但是我很好奇如何在DOS或cmd中进行扩展.

My actual use case is in a bat file with SETX to set global environment variables that rely on another environment variable to be expanded within them but I'm curious as to how to expand in either DOS or cmd.

推荐答案

在批处理文件中,回显%% foo %%将生成%foo%.

in a batch file, echo %%foo%% will generate %foo%.

c:\01Temp>type foo.bat
@echo %%foo%%

c:\01Temp>foo
%foo%

c:\01Temp>

这篇关于如何抑制DOS和Windows CMD中的环境变量扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆