Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Windows undefined environment variable?

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.

like image 324
Larry8811 Avatar asked Mar 16 '17 09:03

Larry8811


People also ask

What is the use of environment variables in Windows?

Environment variables store data that is used by the operating system and other programs. For example, the WINDIR environment variable contains the location of the Windows installation directory. Programs can query the value of this variable to determine where Windows operating system files are located.

What is the purpose of environment variables?

Environment variables help programs know what directory to install files in, where to store temporary files, and where to find user profile settings. They help shape the environment that the programs on your computer use to run.

Is it necessary to set environment variable?

However, setting some environment variables makes some things easier. PATH If the jre/bin folder is on the path, you don't have to qualify to run the java command. If the jdk/bin folder is on the path, you don't have to qualify to run the java and javac commands. As well as some other commands provided by Java.


1 Answers

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.

like image 61
Dávid Molnár Avatar answered Oct 10 '22 07:10

Dávid Molnár