views:

168

answers:

2

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.

+2  A: 

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

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

c:\01Temp>foo
%foo%

c:\01Temp>
cdkMoose
D'Oh! I was thrown off because that doesn't work on the command line if foo is set (%%foo%% expands to %bar% on command line). Is there a way other than the hack (set percent=%) to do it on the command line ?
Adisak
+1  A: 

Within a batch file, use two %%s, e.g.:

set foo=1
echo %%foo%%

...echoes "%foo%", not "1". I'm not aware of a way to disable it in immediate mode (e.g., not in a batch file).

T.J. Crowder
Yeah, I was thrown off because it wasn't working the way I wanted in immediate mode no matter how many %'s I had in a row.
Adisak