0

So basically, all these variables are set to the PLUS symbol ( + )
Echo %1,10% %2,10% %3,10% %4,10% %5,10% %6,10% %7,10% %8,10% %9,10% %10,10% Echo %1,9% %2,9% %3,9% %4,9% %5,9% %6,9% %7,9% %8,9% %9,9% %10,9% Echo %1,8% %2,8% %3,8% %4,8% %5,8% %6,8% %7,8% %8,8% %9,8% %10,8% Echo %1,7% %2,7% %3,7% %4,7% %5,7% %6,7% %7,7% %8,7% %9,7% %10,7% Echo %1,6% %2,6% %3,6% %4,6% %5,6% %6,6% %7,6% %8,6% %9,6% %10,6% Echo %1,5% %2,5% %3,5% %4,5% %5,5% %6,5% %7,5% %8,5% %9,5% %10,5% Echo %1,4% %2,4% %3,4% %4,4% %5,4% %6,4% %7,4% %8,4% %9,4% %10,4% Echo %1,3% %2,3% %3,3% %4,3% %5,3% %6,3% %7,3% %8,3% %9,3% %10,3% Echo %1,2% %2,2% %3,2% %4,2% %5,2% %6,2% %7,2% %8,2% %9,2% %10,2% Echo %1,1% %2,1% %3,1% %4,1% %5,1% %6,1% %7,1% %8,1% %9,1% %10,1%

but for some reason, this is the output: ,102,103,104,105,106,107,108,109,1010,10 ,92,93,94,95,96,97,98,99,910,9 ,82,83,84,85,86,87,88,89,810,8 ,72,73,74,75,76,77,78,79,710,7 ,62,63,64,65,66,67,68,69,610,6 ,52,53,54,55,56,57,58,59,510,5 ,42,43,44,45,46,47,48,49,410,4 ,32,33,34,35,36,37,38,39,310,3 ,22,23,24,25,26,27,28,29,210,2 ,12,13,14,15,16,17,18,19,110,1

Could someone explain to me what's going on because I have no clue (I did verify with the set command that the variables were set to the + symbol and they were)

2 Answers2

3

Avoid starting variable names with a number, this will avoid the variable being mis-interpreted as a parameter

Read Windows Environment Variables as well:

Variables have a percent sign on both sides: %ThisIsAVariable%
The variable name can include spaces, punctuation and mixed case: %_Another Ex.ample%
(This is unlike Parameter variables which only have one % sign and are always one character long: %A, %1 )

For instance, your first line:

Echo %1,10% %2,10% %3,10% %4,10% %5,10% %6,10% %7,10% %8,10% %9,10% %10,10%
rem       ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑    ↑↑↑
rem       % % =  invalid variable name 
rem              nonexistent variables are evaluated to an empty string  in batch script
rem  ↑↑
rem  %1 = the 1st command line parameter supplied to a batch script
rem       evaluates to an empty string if no parameter is  supplied
rem                                 an isolated % percent sign is ignored ↑ 

evaluates to ,102,103,104,105,106,107,108,109,1010,10

Required reading (entire thread): How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
1

JosefZ did a good job explaining what is happening in his answer.

I have a couple more points just to further your understanding.

Batch parameters like %1 are only available within batch scripts. The command line has a slightly different parser that knows nothing about batch parameters. So your code will work as you intended if you enter the commands directly on the command line.

You can get your code to work as originally intended within a batch script if you enable delayed expansion:

@echo off
setlocal enableDelayedExpansion
Echo !1,10! !2,10! !3,10! !4,10! !5,10! !6,10! !7,10! !8,10! !9,10! !10,10!
Echo !1,9! !2,9! !3,9! !4,9! !5,9! !6,9! !7,9! !8,9! !9,9! !10,9!
Echo !1,8! !2,8! !3,8! !4,8! !5,8! !6,8! !7,8! !8,8! !9,8! !10,8!
Echo !1,7! !2,7! !3,7! !4,7! !5,7! !6,7! !7,7! !8,7! !9,7! !10,7!
Echo !1,6! !2,6! !3,6! !4,6! !5,6! !6,6! !7,6! !8,6! !9,6! !10,6!
Echo !1,5! !2,5! !3,5! !4,5! !5,5! !6,5! !7,5! !8,5! !9,5! !10,5!
Echo !1,4! !2,4! !3,4! !4,4! !5,4! !6,4! !7,4! !8,4! !9,4! !10,4!
Echo !1,3! !2,3! !3,3! !4,3! !5,3! !6,3! !7,3! !8,3! !9,3! !10,3!
Echo !1,2! !2,2! !3,2! !4,2! !5,2! !6,2! !7,2! !8,2! !9,2! !10,2!
Echo !1,1! !2,1! !3,1! !4,1! !5,1! !6,1! !7,1! !8,1! !9,1! !10,1!

But... even though you can technically use variable names that begin with a digit, I strongly advise that you should not do this.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390