0

I thought that batch files would allow for empty variable to be output as a null string (""). If I set var1 to nothing, I thought I should

set "var1="
echo Variable value is: %var1%

I thought I'd get this:

Variable value is: 

I actually get this as the output:

Variable value is: %var1%

Why isn't the var1 variable evaluating a blank string? For the record, I tested this with a new CMD instance in Windows 7.

  • Why do you have quotes in the set? – BCartolo Oct 24 '16 at 20:12
  • It's either a string or not a string. You have chosen to set it as not a string, i.e. An undefined variable. – Compo Oct 24 '16 at 21:17
  • 3
    I bet you will get your intended result if you run that code in a batch file. – Squashman Oct 24 '16 at 21:41
  • The quotes are there to show that I didn't have a hidden space or something. It's a valid way of assigning a variable such as: set "path=C:\Program Files" without having quotes in the string text. Anyways, Squashman was right that it did give me the expected output when run in a batch file. I know there are differences between command-line and batch files but I didn't know this one! Thanks! – PaulCKK Oct 24 '16 at 22:56
  • This behaviour shows the difference between the command line and the batch file parser: in command prompt, empty variables are not replaced by empty strings; in batch files, empty variables are expanded to empty strings; see also this great post: [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](http://stackoverflow.com/a/4095133) – aschipfl Oct 25 '16 at 10:43
  • Performing `set "var="` doe not "set variable to empty string", but delete it. If you try to echo non-exist variable, than %var% string is not evaluated because no such variable exist. – user2956477 Oct 26 '16 at 08:11
  • @user2956477, yes, it does delete it but as aschipfl and Squashman pointed out, the deleted variable echoes as an empty string in the batch file parser but not the command line parser. – PaulCKK Oct 26 '16 at 17:47

0 Answers0