Page 1 of 1

<LF> is allowed in variable names

Posted: 02 Jul 2020 12:06
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set ^"line1^

=111^"

set ^"line2^

^

=23432^"

set line

rem Use Delayed Expansion to access these variables:

echo line1=!line1^

!

echo !line2^

^

:4=YES!
Output:

Code: Select all

line1
=111
line2

=23432
line1=111
23YES32
Antonio

Re: <LF> is allowed in variable names

Posted: 02 Jul 2020 13:54
by T3RRY
Interesting find. Call may also be used to expand the variable:

Code: Select all

@Echo Off & Setlocal EnableDelayedExpansion

Set ^"var1^
=_1^"

Set ^"var2^
=_2^"

Set var

Call Echo/[!var1^
!]
Call Echo/[!var2^
!]

Echo/[!var1:_=Var #!]
Echo/[!var2:_=Var #!]

Echo/[!var1^
:_=Var #!]
Echo/[!var2^
:_=Var #!]

Call Echo/[!var1^
:_=Var #!]
Call Echo/[!var2^
:_=Var #!]
Output:

Code: Select all

var1=_1
var2=_2
[_1]
[_2]
[var1:_=Var #]
[var2:_=Var #]
[var1:_=Var #]
[var2:_=Var #]
[Var #1]
[Var #2]

Re: <LF> is allowed in variable names

Posted: 03 Jul 2020 03:56
by Aacini
T3RRY wrote:
02 Jul 2020 13:54
Interesting find. Call may also be used to expand the variable:
Mmmm... Two points about your code.

First, in your example the variable names have NOT a LineFeed in the name:

Code: Select all

@Echo Off & Setlocal EnableDelayedExpansion

Set ^"var1^
=_1^"

Set ^"var2^
=_2^"

Set var

echo %var1%
echo %var2%
Output:

Code: Select all

var1=_1
var2=_2
_1
_2
In the output of Set var command you can see that "var1" and "var2" are followed by an equal sign in the same line and that such a variables can be accessed in the %var1% and %var2% standard way.

To insert a LineFeed in the variable name an additional empty line must be included:

Code: Select all

@Echo Off & Setlocal EnableDelayedExpansion

Set ^"var1^

=_1^"

Set ^"var2^

=_2^"

Set var

echo %var1%
echo %var2%
Output:

Code: Select all

var1
=_1
var2
=_2
ECHO está desactivado.
ECHO está desactivado.


In second place, the Call method to expand a variable is used in combination with double percent signs; NOT with Delayed Expansion:

Code: Select all

@Echo Off & Setlocal

Set ^"var1^

=_1^"

Set ^"var2^

^

=_2^"

Set var

Call Echo %%var1^

:_=Var #%%
Call Echo %%var2^

^

:_=Var #%%
Output:

Code: Select all

var1
=_1
var2

=_2
Var #1
Var #2
In this last example note that var2 have two LineFeeds in its name.

Antonio

PS - The %standard% (one percent) expansion method don't work in this case.

Re: <LF> is allowed in variable names

Posted: 04 Jul 2020 03:49
by T3RRY
All good points, I was missing the obvious in the output from the set display of the variable content, and working with the results of the the variable names that contained carets, not linefeeds.

Re: <LF> is allowed in variable names

Posted: 18 Jul 2020 05:31
by Sponge Belly
See also DT: Safely Expand Any Variable by Dave Benham.