Welcome!

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

SignUp Now!

Can alias pass parameters to FOR command?

Apr
23
0
alias a=echo %*
It work.
>a 1 2 3
1 2 3

alias b=`for %f in (%*) echo %f`
It doesn't work.
>b 1 2 3

output nothing.
 
Last edited:
I'm not sure exactly what's going on here, but it has something to do with the parsing of parentheses:

Code:
C:\>alias b=`echo ( %* )`

C:\>b foo bar fum
(  ) foo bar fum

C:\>alias b=`echo ( %$ )`

C:\>b foo bar fum
( foo bar fum )

C:\>

But the help file only describes %$, not %*. I'm guessing %* is a spillover from batch files. If it works with aliases, it's only by accident. Anyway, that's my guess.
 
This has nothing to do with the parsing of parentheses. Even without parentheses, alias cannot recognize %*.

alias ta=`echo %* tail string`
c:\>ta head string
It will not display
head string tail string
Instead, it shows
tail string head string


Only alias ta=`echo %$ tail string` can correctly display
head string tail string
. In other words, alias completely treats %* as non-existent.

However, in the Help v.25 section 'Alias Parameters', it says:
Aliases can use command line parameters or parameters like those in batch files.

In the 'Batch File Parameters' section, the explanation for %$ and %* is:
%$ the complete command tail, modified by SHIFT
%* the complete command tail, unmodified by SHIFT

If alias could use parameters exactly the same way as batch files, then logically %* should also work, and the meaning of %* is not exactly the same as %$. For some reason, in practice, alias completely does not recognize %* , and the examples in the 'Alias Parameters' section do not mention the use of %* either.
 
Last edited:
%$ expands to all alias parameters. %* expands to all batch parameters. They are analogous, but not the same thing. Note that you can use aliases in a batch file.
 
Code:
@echo off
setlocal

alias test1=`echo "%$"`
alias test2=`echo "%*"`

echo These two aliases look similar, but they do very different things.
echo TEST1 echos ITS OWN parameters, in quotes:
echo.

test1 One two three

echo.
echo But TEST2 echos any BATCH FILE'S parameters, in quotes, followed
echo by its own, if any:
echo.

test2 One two three

echo.
echo Try calling this batch files with different parameters to see how
echo this works.
echo.

endlocal
 
The only thing I found is that %* is not recognized as a "whole" variable. That is, if you echo **%***, you get *****. Are there other shortcomings?

And %* wouldn't enhance aliases any since SHIFT is not valid in an alias.
 
P.S., but you can %[*].

Code:
v:\> type params.btm
echo ..%*..
echo ..%[*]..

v:\> params.btm a b c
..*..
..a b c..
 
There is another difference I discovered. The variable %$ parses the arguments, while %* returns the command tail as entered by the user. Among other things, multiple spaces between arguments condense into single spaces. Here is some of the output from a BTM script VARS.BTM that I wrote to display the values of many batch file parameters. Here it was invoked as vars one two, with three spaces separating the two arguments.

Code:
   %$                 All arguments on command line (as per TCC) .............. one two
   %$                 All arguments on command line (as per TCC) ASCII ........ 111 110 101 32 116 119 111
   %*                 Complete command tail ................................... one   two
   %*                 Complete command tail (all characters) ASCII ............ 111 110 101 32 32 32 116 119 111

There is a bigger difference when arguments are separated by commas, as shown with vars a,b. In one case, commas are stripped and replaced by spaces. As in the above example, %* returns the command tail exactly as the user entered it.

Code:
   %$                 All arguments on command line (as per TCC) .............. a b
   %$                 All arguments on command line (as per TCC) ASCII ........ 97 32 98
   %*                 Complete command tail ................................... a,b
   %*                 Complete command tail (all characters) ASCII ............ 97 44 98
 
The TCC help files should be corrected. The parameter "%*" really is the "complete command tail, unmodified by SHIFT". The parameter "%$", however, is not the "complete command tail, modified by SHIFT". It is the "complete command tail, modified by SHIFT and with multiple parameter separators condensed to a single space".

For example, if the command tail is one, ,two, then %$ is one two, and %* is one, ,two.
 
Slight addition: "%*" is also not exactly the command tail. It strips leading and trailing parameter separators, including commas. Thus a command tail of , one two becomes one two.
 
Back
Top