0

I've been wondering what is the difference between "myvar=me" and "myvar"="me" in a batch file?

It might make a difference to my program which is a rock, paper, and scissors game.

Mofi
  • 38,783
  • 14
  • 62
  • 115

2 Answers2

1

The difference can be easily seen on running a batch file with following lines:

@set "myvar=me"
@set "myvar"="me"
set myvar
@pause

The first line defines an environment variable with name myvar with value me.

The second line defines an environment variable with strange name myvar" with value "me.

The third line is output by Windows command interpreter after preprocessing the command line before execution and outputs all environment variables of which name start with myvar with environment variable name, equal sign and environment variable value.

And fourth line halts batch execution until a key is pressed to see output of third line in case of batch file was executed with a double click.

So the first three lines of output are:

set myvar
myvar=me
myvar"="me

For details on how to define an environment variable right with correct assigning a value read answer on:

Why is no string output with 'echo %var%' after using 'set var = text' on command line?

It explains with text and examples why the syntax set "variable=value" is usually the best.

Mofi
  • 38,783
  • 14
  • 62
  • 115
0

That depends on context.

In case of usage inside the set command it can treat quotes as a part of a variable name and a value:

>set "a"="111"

'

>set
a"="111
...

But not in this case:

>set "a=111"

'

>set
a=111
...

Internal logic of the cmd.exe is simple is that as it eats character by character and removes quotes from the most left and right parts of a string.

Here is another context (file test.bat):

@echo off

call :TEST "1111"="2222"
call :TEST "1111","2222"
call :TEST "1111";"2222"
call :TEST "1111"-"2222"

exit /b 0
:TEST 
echo -%1- -%2-

'

-"1111"- -"2222"-
-"1111"- -"2222"-
-"1111"- -"2222"-
-"1111"-"2222"- --

As you see some characters treated here as a parameter separator in a command line.

Personally I prefer to write the set "var=value" without ^-escape characters before the & and ) characters which can be part of a file path.

Andry
  • 1,237
  • 13
  • 21
  • 2
    This answer is inexact. In all Windows versions the delimiters for command-line parameters are comma, semicolon and equal sign (besides space and TAB) as explicitly documented in many sites, like [this one](https://en.wikibooks.org/wiki/Windows_Batch_Scripting). – Aacini Apr 09 '18 at 00:29