30

I'm doing some simple setting of a variable in a BAT file. It's not setting the variable. There aren't any odd constructs, it's simple variable substitution using the same variable name. I reduced the BAT file to a simple proof of concept version:

set TESTVAR = "No Value"
ECHO var = %TESTVAR%
set TESTVAR = ""
ECHO var = %TESTVAR%
set TESTVAR = "New value"
ECHO var = %TESTVAR%

And the output shows that none of the SET commands seem to be working. What the heck am I missing here. I've been writing BAT files for years and I've never seen this before. Here's the output from running this test:

C:\Users\rs02130\Desktop>test

C:\Users\rs02130\Desktop>set TESTVAR = "No Value"

C:\Users\rs02130\Desktop>ECHO var =
var =

C:\Users\rs02130\Desktop>set TESTVAR = ""

C:\Users\rs02130\Desktop>ECHO var =
var =

C:\Users\rs02130\Desktop>set TESTVAR = "New value"

C:\Users\rs02130\Desktop>ECHO var =
var =
C:\Users\rs02130\Desktop>

I expect the first and third ECHO commands to display the values "No Value" and "New value". What the heck is going on?

4
  • 16
    No one said, that you should put spaces between Testvar and the equal sign. Commented Oct 18, 2013 at 11:26
  • Not sure about this, but maybe you need to EnableDelayedExpansion Commented Oct 18, 2013 at 11:27
  • 10
    Spaces around the equal signs <sigh>... I knew it was something really dumb. Thanks. Commented Oct 18, 2013 at 11:29
  • I just did the same darn thing. How dumb. Commented Apr 26, 2019 at 16:54

1 Answer 1

48

The problem is the spaces around the equal sign. This should do what you want.

set TESTVAR="No Value"
ECHO var = %TESTVAR%
set TESTVAR=""
ECHO var = %TESTVAR%
set TESTVAR="New value"
ECHO var = %TESTVAR%
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.