How to define an MS-DOS variable?

Let's say I do the following in a CMD shell:

set FOO=bar 

Is there any way to define this variable other than recycling the CMD shell?

+52
windows cmd batch-file
Aug 28 2018-11-11T00:
source share
5 answers

Yes, you can disable it with

 set FOO= 

Important Note. Make sure there are no trailing spaces / other invisible characters after the = character. set FOO= ! = set FOO=

+79
Aug 28 2018-11-11T00:
source share

A safe way to disable a variable is to use quotation marks as well, then there is no problem with trailing spaces.

 set FOO=bar echo %FOO% set "FOO=" text after the last quote is ignored echo %FOO% 
+24
Aug 28 '11 at 12:45
source share

This works for me in my Windows 7 CMD shell:

 set FOO=bar echo %FOO% // bar set FOO= echo %FOO% // empty; calling "set" no longer lists it 
+7
Aug 28 2018-11-11T00:
source share

another method

 @Echo oFF setlocal set FOO=bar echo %FOO% endlocal echo %FOO% pause 

Note. This will not work on the interactive command line. But it works in batch mode script.

+7
Aug 28 '11 at 23:10
source share

I would suggest the following only as a comment, but I think it is important enough to stand on my own.

Many previous answers mentioned that you need to beware of lagging spaces; and that's for sure. However, I found that sometimes trailing spaces just want to get there no matter what is especially important if you are running a single-line command line interface and need space as a command separator.

This is the solution to this problem:

 SET FOO=Bar echo %FOO% :: outputs Bar SET "FOO=" echo %FOO% :: outputs %FOO% 

By wrapping the ad in double quotation marks this way, the span issue can be completely eliminated. It can also be very useful when variables are created by concatenating to eliminate spaces between them - for example - paths, for example:

 SET A=c:\users\ && SET D=Daniel SET P="%a%%d%" ECHO %P% :: outputs "C:\Users\ Daniel" :: Notice the undesirable space there 
+1
Dec 01 '16 at 16:15
source share



All Articles