Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 263
Adisak Avatar asked Jan 13 '10 17:01

Adisak


People also ask

How do I set environment variable in CMD?

To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.

How do I find the Windows environment variable in CMD?

On WindowsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter set. A list of all the environment variables that are set is displayed in the command window.

How do you override an environment variable?

In the User variables section, select the environment variable you want to modify. Click Edit to open the Edit User Variable dialog box. Change the value of the variable and click OK. The variable is updated in the User variables section of the Environment Variables dialog box.


1 Answers

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

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

c:\01Temp>foo
%foo%

c:\01Temp>
like image 57
cdkMoose Avatar answered Sep 29 '22 13:09

cdkMoose