Defining and using a variable in batch file

Jamie Dixon picture Jamie Dixon · May 11, 2012 · Viewed 782.5k times · Source

I'm trying to define and use a variable in a batch file. It looks like it should be simple:

@echo off

set location = "bob"
echo We're working with "%location%"

The output I get is the following:

We're working with ""

What's going on here? Why is my variable not being echo'd?

Answer

Brian Nixon picture Brian Nixon · May 11, 2012

The space before the = is interpreted as part of the name, and the space after it (as well as the quotation marks) are interpreted as part of the value. So the variable you’ve created can be referenced with %location %. If that’s not what you want, remove the extra space(s) in the definition.