Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

batch file - How do you suppress environment variable expansion within DOS and Windows CMD?

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.

I am doing the following.

> echo %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%

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

c:1Temp>type foo.bat
@echo %%foo%%

c:1Temp>foo
%foo%

c:1Temp>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...