14

Here's a simple but puzzling question.

For an undefined Windows environment variable, abc for example

In the Command Prompt window ECHO [%abc%] results in [%abc%]

But in a .CMD batch file ECHO [%abc%] results in []

Why the difference? I've researched the ECHO command and can't find anything about this. I'm concerned about where else this subtle difference might apply.

Larry8811
  • 189
  • 1
  • 4
  • 7
    If you are really interested, work through [this answer](http://stackoverflow.com/a/4095133/6811411) highlighting the internal parsing of batch and cmd line interpreter. –  Mar 16 '17 at 09:48
  • 6
    A short summarize: It's independent of the `ECHO` command, the expansion rules differs from cmd context with batch file context – jeb Mar 16 '17 at 10:15
  • 2
    You may make good use of this point in some ways. For example, at the command prompt: `for %a in (*.txt) do @set "var=%a" & call echo File: %var%`. Remember that `var` variable must be undefined in order for this to work... – Aacini Mar 16 '17 at 12:48
  • Thanks for the feedback. I did not know of the parsing difference. I read LotPings "answer" about this. Way too many differences between CMD Window and .CMD file ! Just a short time ago I posed a question about CALL SET differences, which are now probably explained by the parsing difference. Bottom Line: I no longer trust testing anything in CMD Window. Want to test something? Test it in a .CMD file. For example, like copying and pasting code fragments from posts on this site. If you paste them in a CMD Window you may not get the expected results. Anarchy ! – Larry8811 Mar 19 '17 at 18:56
  • I believe [this answer](http://stackoverflow.com/questions/5347593/whats-the-difference-between-bat-and-cmd-file) explains it – Eze Ahuna Apr 03 '17 at 04:05
  • 1
    Possible duplicate of [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) –  May 13 '17 at 01:15
  • [undefined environmental variable expansion in cmd vs batch files](http://stackoverflow.com/q/39707858/995714) – phuclv May 20 '17 at 13:37
  • it's about variable substitution so obviously you won't find anything if you read echo's help http://stackoverflow.com/q/16367814/995714 – phuclv May 20 '17 at 14:17

1 Answers1

1

Really good question! Confusing huh?

There are actually two distinct parsers used to parse batch scripts and command line commands.

Quote from this excellent answer:

BatchLineParser - The parser inside of batch files, for lines or blocks

CmdLineParser - Like the BatchLineParser, but directly at the command prompt, works different

The key difference is in the first phase of parsing, particularly the extension of %var%:

In BatchLineParser if var does not exists will be replaced with nothing, in CmdLineParser if the var isn't defined, the expression will be unchanged.

So why did someone design it this way? I have absolutely no idea.

Community
  • 1
  • 1
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55