<LF> is allowed in variable names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

<LF> is allowed in variable names

#1 Post by Aacini » 02 Jul 2020 12:06

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

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: <LF> is allowed in variable names

#2 Post by T3RRY » 02 Jul 2020 13:54

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]

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: <LF> is allowed in variable names

#3 Post by Aacini » 03 Jul 2020 03:56

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.

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: <LF> is allowed in variable names

#4 Post by T3RRY » 04 Jul 2020 03:49

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.

Sponge Belly
Posts: 216
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: <LF> is allowed in variable names

#5 Post by Sponge Belly » 18 Jul 2020 05:31

See also DT: Safely Expand Any Variable by Dave Benham.

Post Reply