Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Batch Arguments %$ and %*

Jun
1,092
48
There are crucial differences between %$ and %*. In a batch script, the former treats commas as argument separators and %$ then produces a list of the arguments with a single space separating them (no matter how many spaces separated them in the command line). This is not what the help says. The help has the following:

the complete command tail, modified by SHIFT​

It is not the command tail; it is a list of the arguments in the command tail. Thus, my script that simply echoes %$ produces the following:

Code:
[TCC36.51.77 (19284) C:\temp]
->test.btm $4,0     *        2
arg: "$4 0 * 2"

If I change %$ to %* in test.btm, the result is quite different. Now I get the actual command tail.

Code:
[TCC36.51.77 (19284) C:\temp]
->test.btm $4,0     *        2
arg: "$4,0     *        2"

To make matters more confusing, the behavior is not the same with alias parameters. The parameter %* returns a null value (not an error), and the parameter %$ returns what %* does for batch files.

Code:
[TCC36.51.77 (19284) C:\temp]
->al test
set arg=%$ & echo arg: "%arg"

[TCC36.51.77 (19284) C:\temp]
->test $4,0     *        2
arg: "$4,0     *        2"

[TCC36.51.77 (19284) C:\temp]
->alias test & test $4,0     *        2
set arg=%* & echo arg: "%arg"
arg: "" $4,0     *        2

I discovered this when trying to pass arguments to a script that uses @EVAL to do a calculation. If a numerical argument has a comma, it turns into two separate numbers when %$ is used. For a batch file, one has to use %*; for an alias, one has to use %$.

I did just notice that @EVAL now strips commas embedded in numbers (it didn't always do that, did it?), but it does not strip dollar signs. I'm often pasting text from a source that includes both, so I use either a script or alias. The help does say

@EVAL also supports parentheses (to control evaluation order), commas, hexadecimals and decimal separators.​

It's not clear to me what is meant by "supporting commas". As far as I can tell, they are simply ignored (which is what I would want). Perhaps the same could be done for dollar sign characters (not parts of parameters). For now, I am stripping them myself (both commas and dollar signs) in the script or alias.
 
I encountered a difference between TCC 35.00.21 and 36.52.87 with an alias containing %@eval and %$.
For many years I have defined the alias:
alias e= echo %0: %$ = %@eval[%@replace[.,%=,,%$]]
The expression after @alias e= should be enclosed in reverse quote characters and not contain any space:
%@eval[%@replace[.,%=,,%$]]

With 35.00.21 this gives the results:
[]ver
TCC 35.00.21 x64 Windows 10 [Version 10.0.19045.7663]
[]e (1002-954)
e: (1002-954) = 48
[]e 1002-954
e: 1002-954 = 48

With 36.52.87 the results are different:
[]ver
TCC 36.52.87 x64 Windows 10 [Version 10.0.19045.7663]
[]e (1002-954)
e: (1002-954) = 48
[]e 1002-954
e: 1002-954 = -953,8998
 
TCC 36.52.86 x64 Windows 10 [Version 10.0.19045.7663]

From test.btm

Code:
@SETLOCAL
@ECHO OFF

echo %*
echo %$

ENDLOCAL

Result;
Code:
R:\>test.btm 1,120.66    *   52
1,120.66    *   52
1 120.66 * 52

The 1,120.66 now becomes two arguments instead of one argument.

Works the same way in TCC LE

Code:
r:\>ver

TCC LE  14.00.9   Windows 10 [Version 6.3.19045]

r:\>test.btm 1,120.66    *   52
1,120.66    *   52
1 120.66 * 52

r:\>


Joe
 
Back
Top