Welcome!

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

SignUp Now!

Brackets Around Variable Names

Jun
539
3
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.
 
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**
 
You can enclose a variable name in brackets to force TCC to accept what would normally be invalid variable name characters (i.e., %[ab@cd]).

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:
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
 
You can enclose a variable name in brackets to force TCC to accept what would normally be invalid variable name characters (i.e., %[ab@cd]).

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?
 
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
 
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.
 
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$
 
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$
 

Similar threads

Back
Top