Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what's wrong with my script? it is about set a null variable

22 views
Skip to first unread message

mande...@gmail.com

unread,
Apr 18, 2008, 3:04:39 AM4/18/08
to
Dear all,

I have the following commands run in a command prompt:


C:\>set abc=
C:\>if "%abc%"=="" (echo abc is null)

C:\>

I dont understand why the above command would not echo "abc is null".

Thanks a lot!

Regards

Esra Sdrawkcab

unread,
Apr 18, 2008, 3:11:10 AM4/18/08
to

Check that the "set abc=" line doesn't have a trailing space.

mande...@gmail.com

unread,
Apr 18, 2008, 3:21:40 AM4/18/08
to
Dear all,

I have the following commands run in a command prompt and the result
is as follow:

C:\>set abc=

C:\>if "%abc%"=="" (echo abc is null)

C:\>


I don't understand why it would not echo "abc is null" finally.

Thanks a lot.

Regards,

foxidrive

unread,
Apr 18, 2008, 3:31:06 AM4/18/08
to
On Fri, 18 Apr 2008 00:04:39 -0700 (PDT), mande...@gmail.com wrote:


> I have the following commands run in a command prompt:
>
>C:\>set abc=
>C:\>if "%abc%"=="" (echo abc is null)
>

>I dont understand why the above command would not echo "abc is null".

It fails here too at the command line but works in a batch file.

XP Pro SP2

billious

unread,
Apr 18, 2008, 7:29:44 AM4/18/08
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:o7jg04lqvqs6hobmd...@4ax.com...

Hmm.

Try, at the prompt:

echo %abc%

now - isn't that interesting?


Tom Lavedas

unread,
Apr 18, 2008, 8:29:56 AM4/18/08
to

The 'apparent' variable is not being expanded as a variable at the
command prompt. It would appear that expansion only occurs for
variables that are actually SET - clearly, a flaw. Therefore it
remains a literal string in the evaluation of the IF statement - and
"%abc%" is, in fact, NOT equal to "".

If you really need it to work at the command prompt (in NT/2K/2003/XP/
Vista?/2008?) use the other syntax ...

if not defined abc (echo abc is null)

However, there is little reason for this to be required to work at the
command prompt, so I suppose it's of little consequence. It does work
correctly in batch procedures, as has been pointed out earlier.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

foxidrive

unread,
Apr 18, 2008, 8:36:27 AM4/18/08
to
On Fri, 18 Apr 2008 19:29:44 +0800, "billious" <billio...@hotmail.com>
wrote:


That's normal for an undefined variable, although now that you point it out
it is odd.

c:\>set abc=

c:\>echo %abc%
%abc%

c:\>set abc=a

c:\>echo %abc%
a

foxidrive

unread,
Apr 18, 2008, 8:44:32 AM4/18/08
to
On Fri, 18 Apr 2008 05:29:56 -0700 (PDT), Tom Lavedas <tglb...@cox.net>
wrote:

>> C:\>set abc=
>> C:\>if "%abc%"=="" (echo abc is null)
>
>The 'apparent' variable is not being expanded as a variable at the
>command prompt. It would appear that expansion only occurs for
>variables that are actually SET - clearly, a flaw. Therefore it
>remains a literal string in the evaluation of the IF statement - and
>"%abc%" is, in fact, NOT equal to "".

I'm glad you guys put a reason to the issue - I had shrugged my shoulders
and put it down to the ways of Windows.

John Gray

unread,
Apr 18, 2008, 9:21:47 AM4/18/08
to

I don't understand either, but rather than waiting for MS to fix the
code of CMD.EXE, pragmatically try something equivalent:
if not defined abc echo abc is null
(Yes, not %abc%...)

mande...@gmail.com

unread,
Apr 18, 2008, 10:04:18 AM4/18/08
to
On Apr 18, 3:11 pm, Esra Sdrawkcab <ad...@127.0.0.1> wrote:

I am sure that "set abc=" line doesnt have a trailing space. You may
copy and paste my code to try. Anyway, thank a lot for your reply :)

mande...@gmail.com

unread,
Apr 18, 2008, 10:58:31 AM4/18/08
to
On Apr 18, 3:31 pm, foxidrive <got...@woohoo.invalid> wrote:

> On Fri, 18 Apr 2008 00:04:39 -0700 (PDT), mander.l...@gmail.com wrote:
> >  I have the following commands run in a command prompt:
>
> >C:\>set abc=
> >C:\>if "%abc%"=="" (echo abc is null)
>
> >I dont understand why the above command would not echo "abc is null".
>
> It fails here too at the command line but works in a batch file.
>
> XP Pro SP2

Hi foxidrive. But I have tried in Windows 2003 Server with SP2, it
does not work in batch file

foxidrive

unread,
Apr 18, 2008, 11:45:01 AM4/18/08
to

Well here's my dos window - so there is no issue here.

You can use "if not defined abc (echo abc is null)" as has been suggested.

===[screen capture]===
M:\>a
abc is null
Press any key to continue . . .

M:\>type a.bat
@echo off
set abc=


if "%abc%"=="" (echo abc is null)

pause
M:\>
===[screen capture]===

Tom Lavedas

unread,
Apr 18, 2008, 1:11:47 PM4/18/08
to

This works for me in a batch in XP Pro SP2 ...

set abc=something
(set abc=)


if "%abc%"=="" (echo abc is null)

I must surmise, therefore, the issue is that trailing space that
foxidrive mentioned earlier.

Timo Salmi

unread,
Apr 18, 2008, 2:30:11 PM4/18/08
to
mande...@gmail.com wrote:
> C:\>set abc=
> C:\>if "%abc%"=="" (echo abc is null)
> I don't understand why it would not echo "abc is null" finally.

It does.
89} All of a sudden "echo." doesn't work any more. What's wrong?
209940 Mar 6 2008 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Http link format http://garbo.uwasa.fi/pub/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Tom Lavedas

unread,
Apr 18, 2008, 3:17:51 PM4/18/08
to
On Apr 18, 2:30 pm, Timo Salmi <t...@uwasa.fi> wrote:

> mander.l...@gmail.com wrote:
> > C:\>set abc=
> > C:\>if "%abc%"=="" (echo abc is null)
> > I don't understand why it would not echo "abc is null" finally.
>
> It does.
> 89} All of a sudden "echo." doesn't work any more. What's wrong?

No, Timo, it really doesn't - you have got to try it to believe it. I
didn't think it was true either, until I checked it out. It is a
definite bug in the way the command processor works - at the command
prompt, IMHO. The item you recommend is not really applicable here.

Timo Salmi

unread,
Apr 18, 2008, 5:47:01 PM4/18/08
to

"what's wrong with my _script_?"

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland

Useful script tricks http://www.netikka.net/tsneti/info/tscmd.htm

foxidrive

unread,
Apr 19, 2008, 4:02:44 AM4/19/08
to
On Sat, 19 Apr 2008 00:47:01 +0300, Timo Salmi <t...@uwasa.fi> wrote:

>Tom Lavedas <tglb...@cox.net> wrote:
>> On Apr 18, 2:30 pm, Timo Salmi <t...@uwasa.fi> wrote:
>>> mander.l...@gmail.com wrote:
>>>> C:\>set abc=
>>>> C:\>if "%abc%"=="" (echo abc is null)
>>>> I don't understand why it would not echo "abc is null" finally.
>>> It does.
>>> 89} All of a sudden "echo." doesn't work any more. What's wrong?
>
>> No, Timo, it really doesn't - you have got to try it to believe it. I
>> didn't think it was true either, until I checked it out. It is a
>> definite bug in the way the command processor works - at the command
>> prompt, IMHO. The item you recommend is not really applicable here.
>
>"what's wrong with my _script_?"

The OP was speaking of command line usage though, Timo. :)

Splunger@jizzblaster.nut Arturio Splunger

unread,
Apr 20, 2008, 9:54:03 PM4/20/08
to

set abc= unassigns the env-var abc so it 'doesn't exist'
if "%abc%"=="" (echo abc is null) then tries to test an env-var that is not assigned and fails

Tom Lavedas

unread,
Apr 21, 2008, 8:07:02 AM4/21/08
to
On Apr 20, 9:54 pm, Arturio Splunger <Arturio

Splun...@JizzBlaster.Nut> wrote:
> On Sat, 19 Apr 2008 18:02:44 +1000, foxidrive <got...@woohoo.invalid> wrote:
> >On Sat, 19 Apr 2008 00:47:01 +0300, Timo Salmi <t...@uwasa.fi> wrote:
>
> >>Tom Lavedas <tglba...@cox.net> wrote:
> >>> On Apr 18, 2:30 pm, Timo Salmi <t...@uwasa.fi> wrote:
> >>>> mander.l...@gmail.com wrote:
> >>>>> C:\>set abc=
> >>>>> C:\>if "%abc%"=="" (echo abc is null)
> >>>>> I don't understand why it would not echo "abc is null" finally.
> >>>> It does.
> >>>> 89} All of a sudden "echo." doesn't work any more. What's wrong?
>
> >>> No, Timo, it really doesn't - you have got to try it to believe it. I
> >>> didn't think it was true either, until I checked it out. It is a
> >>> definite bug in the way the command processor works - at the command
> >>> prompt, IMHO. The item you recommend is not really applicable here.
>
> >>"what's wrong with my _script_?"
>
> >The OP was speaking of command line usage though, Timo. :)
>
> set abc= unassigns the env-var abc so it 'doesn't exist'
> if "%abc%"=="" (echo abc is null) then tries to test an env-var that is not assigned and fails

Yes, but the point is that the test is supposed to report that the
variable is not set. In a batch procedure, that is exactly what
happens. However, the problem arises at the command prompt, where the
reverse is true - the result of the IF indicates that the variable is
SET, when it is not. Clearly, an IF statement is not a common
occurrence at the command prompt, but, still, the inconsistency is
troubling.

Scott Seligman

unread,
Apr 21, 2008, 2:26:00 PM4/21/08
to
Tom Lavedas <tglb...@cox.net> wrote:
>Yes, but the point is that the test is supposed to report that the
>variable is not set. In a batch procedure, that is exactly what
>happens. However, the problem arises at the command prompt, where the
>reverse is true - the result of the IF indicates that the variable is
>SET, when it is not. Clearly, an IF statement is not a common
>occurrence at the command prompt, but, still, the inconsistency is
>troubling.

Welcome to the fun of cmd.exe. There are a few things that it treats
differently at the command prompt than it does in batch files, this is
just one of them.

--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Don't walk behind me, I may not lead. Don't walk in front of me, I
may not follow. Just walk beside me and be my friend.
-- Albert Camus

foxidrive

unread,
Apr 21, 2008, 2:53:04 PM4/21/08
to
On 21 Apr 2008 11:26:00 -0700, "Scott Seligman" <seli...@example.com>
wrote:

>Tom Lavedas <tglb...@cox.net> wrote:
>>Yes, but the point is that the test is supposed to report that the
>>variable is not set. In a batch procedure, that is exactly what
>>happens. However, the problem arises at the command prompt, where the
>>reverse is true - the result of the IF indicates that the variable is
>>SET, when it is not. Clearly, an IF statement is not a common
>>occurrence at the command prompt, but, still, the inconsistency is
>>troubling.
>
>Welcome to the fun of cmd.exe. There are a few things that it treats
>differently at the command prompt than it does in batch files, this is
>just one of them.

It's not by design though - it's clearly a bug. A reproducible one. :)

Todd Vargo

unread,
Apr 21, 2008, 7:28:15 PM4/21/08
to
foxidrive wrote:

OTOH, since IF DEFINED var is available, perhaps one should use that method.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Scott Seligman

unread,
Apr 21, 2008, 8:47:25 PM4/21/08
to

I don't like this behavior (nor any of the other differences), but why
is it a bug? Is there's documentation that claims it shouldn't act
this way?

(No doubt it was a bug at one time, but much of cmd.exe's processing
is trying to emulate older versions correctly)

--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------

Never underestimate the bandwidth of a station wagon full of tapes
hurtling down the highway.
-- Andrew S. Tanenbaum

foxidrive

unread,
Apr 21, 2008, 11:53:47 PM4/21/08
to
On 21 Apr 2008 17:47:25 -0700, "Scott Seligman" <seli...@example.com>
wrote:

>>>Welcome to the fun of cmd.exe. There are a few things that it treats


>>>differently at the command prompt than it does in batch files, this is
>>>just one of them.
>>
>>It's not by design though - it's clearly a bug. A reproducible one. :)
>
>I don't like this behavior (nor any of the other differences), but why
>is it a bug?

Because it breaks standard batch logic, as the OP demonstrated.

Here is how Win98se handles it

===[screen capture]===
Microsoft(R) Windows 98
(C)Copyright Microsoft Corp 1981-1999.

C:\>set abc=123

C:\>echo.%abc%
123

C:\>set abc=

C:\>echo.%abc%


C:\>
===[screen capture]===


Can anybody say if NT or W2K works in the same was as XP?

Todd Vargo

unread,
Apr 22, 2008, 4:52:18 PM4/22/08
to
foxidrive wrote:

E:\>ver & rem win95cmd.exe in Windows 98

Microsoft Windows 2000 [Version 4.10.0000]

E:\>set abc=123

E:\>echo %123%
%123%

E:\>set abc=

E:\>echo %123%
%123%

0 new messages