Quoting Percent in CMD.EXE to echo the name of an Environment variable
 

C:\>echo The current value of %^WINDIR^% is %WINDIR%
The current value of %WINDIR% is C:\WINDOWS

 

 

Well, putting a caret before character the first character of  Environment variable name  and after the last character works.


Also,  just a character at either end appears to work too.

 

 
C:\>echo %WINDIR^%
%WINDIR%

C:\>echo %^WINDIR%
%WINDIR%


Interestingly, it looks like CMD.EXE's environment variable expansion gives the %VAR% the value %VAR% if the variable doesn't exist.  When we goes %^WINDIR^% we are apparently trying to access an environment variable with percents in the name, so instead of WINDIR, it is %WINDIR%


C:\>set %^WINDIR^%=someplace

C:\>set
%WINDIR%=someplace
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\student\Application Data
[... lots of variables...]
USERNAME=s
USERPROFILE=C:\Documents and Settings\s
windir=C:\WINDOWS

C:\>


See we have both %WINDIR% and windir.


The amusing thing is that we cannot retreive the value through command line expansion.

C:\>echo %WINDIR%
C:\WINDOWS


C:\>echo %%WINDIR%%
%C:\WINDOWS%

C:\>echo %^WINDIR^%
%WINDIR%

C:\>echo %^%WINDIR%^%
%C:\WINDOWS% 

C:\>echo !WINDIR!
!WINDIR!

C:\>echo %^!WINDIR!^%
%!WINDIR!%

C:\>


We can set and unset P though, and show that the expansion of an unset variable is the percent, the unset variable name, and the percent.


C:\>echo %P%
%P%

C:\>set P=A

C:\>echo %P%
A

C:\>SET P=

C:\>echo %P%
%P%

C:\>

It looks like we can get an interesting effect if we have a trailing caret ^:

C:\>SET P=^
More? )

C:\>echo %P%
)

C:\>SET P=^
More?
More? )

C:\>echo %P%
ECHO is on.

C:\>


Here the variable P seems to have evaluated to nothing as if we had just typed echo by itself.

That is P has the value of  an empty string.

 
C:\>echo
ECHO is on.

C:\>echo of
echo
ECHO is off
echo on

C:\>echo
ECHO is on.

C:\>