CMD vs.Batch: different output

May 28, 2010 at 19:22:37
Specs: Windows 7 x64, 4 GB
First question:

Let's presume variable lol doesn't exist.

echo %lol%
inside cmd window will output:

on the other side...

echo %lol%
inside batch file will output echo status.

In cmd "%lol%" converts to "%lol%".
in batch "%lol%" converts to "".

Why is that so?

Second question:

Windir should be existing variable.

echo ^%windir^%
inside cmd windows will output


echo ^%windir^%
inside batch will output the echo of next line...

Again, why is there a difference?

Third question:

Why do we need to escape "%" in for command in batch files, while in cmd we don't?

Fourth question:

How to output exclamation mark if delayed expansion is enabled?

See More: CMD vs.Batch: different output

Report •

May 29, 2010 at 15:55:54
i can't answer the first three. as for number four, i have used:
@echo off & setlocal enabledelayedexpansion
set ok=OH KAY
echo Pre prc: %ok%
echo Pre exclms: !ok!
setlocal disabledelayedexpansion
echo dis prc: %ok%
echo dis exclms: !ok!
setlocal enabledelayedexpansion
echo post, percent: %ok%
echo post, exclm: !ok!
::----- end
as you can see, the var.s retain their values over the "gap".
however, in a loop, you run a danger of stack overflow for
accumulative setlocals ("setlocal recursion limit exceeded"),
so you would have to carefully manipulate endlocal command
such that you don't lose var.s you need. endlocal kills all
var. values at the "current" level, but not lower levels.

Report •

May 29, 2010 at 18:29:50
I think you misunderstood my question.
Things you wrote I'm quite aware of and familiar with.

My question was:
How to output exclamation mark if delayed expansion is enabled?

If you run this script...

@echo off
setlocal enabledelayedexpansion
echo I want exclamation mark here: ! but it's missing.
echo Exclamation mark visible here: !
pause >nul'll notice that exclamation mark (!) isn't being displayed when delayed expansion is enabled.

So I'm asking, how to echo en exclamation mark (escape it somehow) inside a code where delayed expansion is enabled?
Disabling delayed expansion temporally and enabling it again is not an option.

Report •

May 29, 2010 at 21:25:43
It's really hard to answer most of these, because your asking why on most of them, the only people that would really know why are the people that programmed the cmd/batch environment....

Most of these are guesses based on my observations, but I think all of it could be attributed to a complex or multiple depth command parsing.


I'm not sure, but it can be quite useful to have empty variables in scripts at times. Especially when making commands up from variables.


My guess on this one is that "%windir^%" is being expanded to a empty variable and the first caret is escaping the new line.

echo the caret can also escape a ^
echo %%windir%%

The carets do indeed seem to be the only way to escape the %'s on the command line......


My guess would be to differentiate between parameters %1 and for variables(which can be %%1, but are normally letters).


echo Exclamation mark visible here: ^^!

It's important to remember that cmd tries to be backwards compatible with most stuff back to DOS, but with some new bits and pieces added on. So it could be as simple as it's how it was done in DOS and they didn't want to break compatibility...

Report •

Related Solutions

May 29, 2010 at 22:43:47
aagh! All these trials, having no knowledge.
"we who are not worthy..."
Who would have guessed ^^, not me!
thanks Judago!

Report •

May 29, 2010 at 23:06:58

You're far too kind, I wouldn't have guesses that one either, I read it on some other site quite a while ago.

Report •

May 29, 2010 at 23:44:50
Doesn't diminish my response. Thanks to all here, I've learned a TON.

Report •

May 30, 2010 at 06:59:42
Thank you guys, you are always so helpful. :D

The reason I was asking these questions is because I usually tend to test something in CMD before adding it to batch file. And then, when I think I got something right, it doesn't work in batch.

I wonder if all these stuff are documented somewhere... :/

It's quite a pain when you have to find these things by trial and error.

Report •

Ask Question