101

Let's say I execute the following in the CMD shell:

SET "FOO=bar"

Is there a way to undefine this variable, other than recycling the CMD shell?

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
user288004
  • 1,111
  • 2
  • 7
  • 3
  • 11
    Do you **really** mean MS-DOS (which has been dead for at least 10 years or so) - or do you say *MS-DOS* but really mean the Windows command line (`cmd.exe`) ?? – marc_s Aug 28 '11 at 07:54
  • 5
    Likely, but it doesn't really matter for that question... – Madara's Ghost Aug 28 '11 at 09:17
  • 4
    Marc's right. This is not the MS-DOS shell. People never learn that this is a big difference. – Michael-O Aug 28 '11 at 09:32
  • @MadaraUchiha, Indeed, your `set var=` answer works under MS-DOS 6.22 command.com just the same as it does under Windows cmd.exe. (However, unlike Windows, putting double quotes around the var= doesn't work, instead it creates an environment variable whose name starts with a double quote.) – Simon Kissane Jul 13 '16 at 11:38

5 Answers5

154

Yes, you can unset it with

set FOO=

Or explicitly using:

set "FOO="

Ensure no trailing extraneous (invisible) characters come after the = sign. That is:

  • set FOO= is different from set FOO=      .
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
33

A secure way to unset the variable is to use also quotes, then there aren't problems with trailing spaces.

set FOO=bar
echo %FOO%
set "FOO=" text after the last quote is ignored
echo %FOO%
jeb
  • 78,592
  • 17
  • 171
  • 225
13

another method

@Echo oFF

setlocal
set FOO=bar
echo %FOO%
endlocal

echo %FOO%

pause

Note: This would not work on an interactive command prompt. But works in batch script.

anishsane
  • 20,270
  • 5
  • 40
  • 73
walid2mi
  • 2,704
  • 15
  • 15
10

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
chappjc
  • 30,359
  • 6
  • 75
  • 132
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    Spaces are significant in the `set` command. If you try `set FOO = bar` and then try `set FOO=`, it won't unset 'FOO '. For some reason, though, they aren't (as?) significant inside `% %`. – Ted Hopp Aug 28 '11 at 08:00
  • The trailing space is also important. Sorry for the edit, I thought your rep said 270, not 270k... oops. I often edit answers when rep suggests the user isn't coming back. Obviously not the case here. My mistake. – chappjc Feb 10 '15 at 01:10
  • @chappjs no problem, thanks for the edit! Not sure how I missed that. – Pekka Feb 10 '15 at 08:09
4

I would offer the following only as a comment, but I think it's important enough to stand on its own.

A lot of the previous answers mentioned that one needs to beware of trailing spaces; and for sure that is true. However I've found that sometimes trailing spaces just want to get in there no matter what - particularly if you are doing a command line one-liner and need the space as a command separator.

This is the solution to that problem:

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

By wrapping the declaration in double quotes that way, the spacing issue can be avoided entirely. This can also really useful when variables are created by concatenating to eliminate spaces in between - for example - paths, e.g:

SET A=c:\users\ && SET D=Daniel
SET P="%a%%d%"
ECHO %P%
:: outputs "C:\Users\ Daniel"
:: Notice the undesirable space there
dgo
  • 3,877
  • 5
  • 34
  • 47