Brackets Around Variable Names

Jun 2, 2008
284
1
Newton, MA
#1
Is it legal to use brackets around argument variables? While trying to solve Vince's grep alias problem, I experimented with the following command-line definitions:

alias alias1=`echo %1`
alias alias2=`echo %[1]`​

When I run

alias1 test & alias2 test
I get the following output:

test
test
Note that the version with the brackets produces a leading space.
 
#2
I tried it too. It works in a BTM file but not in an alias (v16 and v17). I wonder why!
Code:
v:\> alias arg1
echo **%[1]**

v:\> arg1 foo
**** foo

v:\> type escape.btm
echo %%[1] is **%[1]**
v:\> escape.btm foo
%[1] is **foo**
 

rconn

Administrator
Staff member
May 14, 2008
10,646
97
#3
You can enclose a variable name in brackets to force TCC to accept what would normally be invalid variable name characters (i.e., %[[email protected]]).

You can also enclose a variable name in brackets to tell TCC it is an expression, and to evaluate it twice (first the expression, then the result).

So yes, you can enclose a "1" in brackets -- but it would be dumb.
 
Last edited:
Jun 2, 2008
284
1
Newton, MA
#5
So yes, you can enclose a "1" in brackets -- but it would be dumb.
I was thinking of a situation in which one wanted to append text to a parameter such as %1, e.g., %1test. The latter could be understood to reference a variable named "1test", so one might want to write "%[1]test" to make it clear. I have not experimented with this, however.

If %1 in an alias is a "parameter" and not a "variable", what about with a BTM script? From what Vince reported, that syntax does work with BTMs.

-- Jay
 
#6
You can enclose a variable name in brackets to force TCC to accept what would normally be invalid variable name characters (i.e., %[[email protected]]).

You can also enclose a variable name in brackets to tell TCC it is an expression, and to evaluate it twice (first the expression, then the result).

So yes, you can enclose a "1" in brackets -- but it would be dumb.
It does serve to defeat the special meaning of '$'. I think that was Jay's intention.
Code:
v:\> escape.btm foo bar
%[1]$ is **foo$**
%1$ is **foo bar**
An alias parameter is not a variable.
Is a batch parameter a variable?
 

rconn

Administrator
Staff member
May 14, 2008
10,646
97
#7
It does serve to defeat the special meaning of '$'. I think that was Jay's intention.
Code:
v:\> escape.btm foo bar
%[1]$ is **foo$**
%1$ is **foo bar**
I don't understand your point -- are you happy about it or complaining about it?

Is a batch parameter a variable?
Yes (albeit not an environment variable).
 

rconn

Administrator
Staff member
May 14, 2008
10,646
97
#8
I was thinking of a situation in which one wanted to append text to a parameter such as %1, e.g., %1test. The latter could be understood to reference a variable named "1test", so one might want to write "%[1]test" to make it clear. I have not experimented with this, however.
Batch variable substitution already works that way.
 
Jun 2, 2008
284
1
Newton, MA
#9
Batch variable substitution already works that way.
I would have expected an alias to work the same way as a batch file. I certainly think of them as conceptually similar.

I suppose it's no big deal to make the alias invoke a batch file to get around this irregularity in parameter interpretation.

-- Jay
 

rconn

Administrator
Staff member
May 14, 2008
10,646
97
#10
I would have expected an alias to work the same way as a batch file. I certainly think of them as conceptually similar.
The similarity begins and ends with both starting with a % followed by a digit. Alias parameters cannot be substituted in another command, shifted, nested, watched in the debugger, or reassigned. They're not even expanded by the variable expansion code.
 
#11
The similarity begins and ends with both starting with a % followed by a digit. Alias parameters cannot be substituted in another command, shifted, nested, watched in the debugger, or reassigned. They're not even expanded by the variable expansion code.
That distinction is nice to know. So an alias can get around the difference like this.
Code:
v:\> alias test `set arg1=%1 & echo %[arg1]$`

v:\> test foo
foo$
 
#12
And another solution to (for example) appending a '$' ... (and at the same time suppressing the appending of %2$ if that's desired) ...
Code:
v:\> alias test `echo %@word[0,%1$]$`

v:\> test foo
foo$

v:\> test foo bar
foo$