Welcome!

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

SignUp Now!

Alias fails with unprotected ^ in %$

May
13,828
211
What's happening below and how fo I fix it?

Code:
v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo %$]`

v:\> myecho ^e[
TCC: Syntax error "@len[^e["
 
Same here ... hmm ... will investigate a bit time here ...
 
It seems, ALIAS tries to execute the ANSI sequence (in contrary to the "normal" @len function). So if I escape the square bracket, myecho alias executes it.

Code:
myecho ^e^[

It produces a blank line.
 
I think it's ECHO that escapes the [.

1737840355532.webp


But why is there an error in the first place?
 
It only seems to happen when there are multiple occurrences of %$ in the alias definition, one being in a function.

Code:
v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo %$]`

v:\> myecho ^e[92mfoo
TCC: Syntax error "@len[^e[92mfoo"

v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo bar]`

v:\> myecho ^e[92mfoo
TCC: Syntax error "@len[^e[92mfoo"

v:\> alias myecho `%@if[%@len[%$] LT 1000,echo bar,echo bar]`

v:\> myecho ^e[92mfoo
bar
 
It only seems to happen when there are multiple occurrences of %$ in the alias definition, one being in a function.

Code:
v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo %$]`

v:\> myecho ^e[92mfoo
TCC: Syntax error "@len[^e[92mfoo"

v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo bar]`

v:\> myecho ^e[92mfoo
TCC: Syntax error "@len[^e[92mfoo"

v:\> alias myecho `%@if[%@len[%$] LT 1000,echo bar,echo bar]`

v:\> myecho ^e[92mfoo
bar
Oh, that's very interesting and good "debugged"!

That's not WAD probably!
 
And IF works better that @IF.

Code:
v:\> alias myecho `%@if[%@len[%$] LT 1000,echo %$,echo %$]`

v:\> myecho ^e[92mfoo
TCC: Syntax error "@len[^e[92mfoo"

v:\> alias myecho `if %@len[%$] LT 1000 (echo %$) else (echo %$)`

v:\> myecho ^e[92mfoo
foo
 
It's OK. IF, instead of @IF works fine.

But I still don't really understand the problem. Why is it not a problem here? Is it peculiar to %$?

Code:
v:\> set z=[

v:\> alias myecho `echo %@if[%@len[%z] NE 0,%z,%z]`

v:\> myecho
[
 
So what's the difference between these? They're the same except that the first uses %$ inside %IF and the second uses %z?

Code:
v:\> alias myecho `set z=%$ & %@if[%@len[%$] NE 0,echo %$,echo %$]`

v:\> myecho [
TCC: Syntax error "@len[["

v:\> alias myecho `set z=%$ & %@if[%@len[%z] NE 0,echo %z,echo %z]`

v:\> myecho [
[
 
So what's the difference between these? They're the same except that the first uses %$ inside %IF and the second uses %z?

Code:
v:\> alias myecho `set z=%$ & %@if[%@len[%$] NE 0,echo %$,echo %$]`

v:\> myecho [
TCC: Syntax error "@len[["

v:\> alias myecho `set z=%$ & %@if[%@len[%z] NE 0,echo %z,echo %z]`

v:\> myecho [
[

The %$ is an alias variable, and is expanded during alias expansion. The %z is an environment variable, and is expanded during variable expansion (which occurs later).
 
Back
Top