variable set is whack

Hewlett-packard / Rc659aa-aba a1632x
August 8, 2009 at 19:12:22
Specs: Microsoft Windows Vista Home Premium, 2 GHz / 2045 MB
heres a simple (and useless) batch code.

@echo off
set 2after=1
echo %2after%
pause

can anyone tell me why it echo's "after" and not "1"?


See More: variable set is whack

Report •


#1
August 8, 2009 at 19:35:17
As is noted here http://ss64.com/nt/set.html

Avoid starting variable names with a number, this will avoid the variable being mis-interpreted as a parameter
%123_myvar% < > %1 23_myvar


Report •

#2
August 9, 2009 at 01:28:40
I'm glad OP brought that up. For a long time I've been thinking that a leading num was a bad idea but didn't quite know why.

Thanks to Wahine for the info.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
August 9, 2009 at 01:34:00
If you really fell you must use a leading num, do it this way:

================================
@echo off & setLocal EnableDELAYedExpansion

set 2after=1
echo !2after!


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
August 9, 2009 at 02:33:32
Thanks Mech2go. Just for interest can you find a workaround (or an explanation) for the output of this? It looks like octal strikes again when the answer should be a simple 4, but why?

:: ++++++
@echo off & setLocal EnableDELAYedExpansion

set 2after=1
echo 2 after = !2after!
echo.
set /a 3after=!2after!*4
echo 3 after = !3after!
:: ++++++


Report •

#5
August 9, 2009 at 02:58:34
"looks like octal strikes again"

yep


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
August 9, 2009 at 03:40:04
"looks like octal strikes again"

I tend to disagree, it seems that set /a is hard coded to not accept variables that start with numbers(obviously).

It seems to really be complaining about the letters after 3, perhaps if the first token starts with a number it expects no variable assignment to be involved. This can be further tested by "set /a 3 = 2+1" where it complains of a missing operator instead.

[edit]
Validly check seems to be the most likely cause because all notations must start with a number, "0x" for hex, "0" for octal and 1-9 for decimal.
[/edit]


Report •


Ask Question