Unbalanced parentheses in variable expansion

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,495
91
Albuquerque, NM
prospero.unm.edu
I don't understand this:

Code:
C:\>set open=(test

C:\>set close=test)

C:\>echo %open %close
(test %close

C:\>

It seems that the contents of the first variable can prevent variable expansion later on the command line. Is this behavior intended? If so, is there a workaround? I've tried using %[TEST], SETDOS /X-5, SETDOS /X-7.
 
Aug 2, 2011
258
4
Berlin, Germany
simple but funny.
CMD can handle it.
Code:
(system)  C:\...\TCMD14x64 >cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.
 
(%@label[])  set open=(test
 
(%@label[])  set close=test)
 
(%@label[])  echo %open% %close%
(test test)
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
It seems that the contents of the first variable can prevent variable expansion later on the command line. Is this behavior intended?

Yes.

If so, is there a workaround? I've tried using %[TEST], SETDOS /X-5, SETDOS /X-7.

Yes -- the one you didn't try (SETDOS /X-4). This behavior is due to nested variable expansion being turned off inside (potential) command groups.
 
May 20, 2008
11,536
103
Syracuse, NY, USA
Yes.



Yes -- the one you didn't try (SETDOS /X-4). This behavior is due to nested variable expansion being turned off inside (potential) command groups.
Code:
v:\> set a=(
v:\> echo %a %a
( %a
I am having a tough time trying to understand your explanation. First, where's the nested variable? And second, if the problem is that nested variable expansion is off, how does turning nested variable expansion **OFF** fix it?
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
Code:
v:\> set a=(
v:\> echo %a %a
( %a
I am having a tough time trying to understand your explanation. First, where's the nested variable? And second, if the problem is that nested variable expansion is off, how does turning nested variable expansion **OFF** fix it?

The parser passes the line through the variable expansion multiple times (until no more expansions occur). The first pass looks like:

echo ( %a

It then looks for the next variable, starting at the point of the first substitution (if nested expansion is enabled):

( %a

The parser sees that the beginning of the argument is a (, and that we do *not* want to do nested variable expansion inside a command group (or FOR argument list), so it moves the point to look for the next variable to just after the trailing ) or end of line, and the result is:

echo ( %a

By turning off nested variable expansion, the parser skips the entire length of the first expanded variable, so the second pass looks like:

%a

which is expanded to:

(

resulting in:

echo ( (

(There is no way this is going to be changed, as among other things it would break every batch file that used variables in FOR arguments.)
 
May 20, 2008
11,536
103
Syracuse, NY, USA
I'll have to chew on that for a while.

Then why isn't the this_might_be_a_command_group mechanism triggered if I just do this?
Code:
v:\> set a=0
v:\> echo ( %a
( 0
 
May 20, 2008
11,536
103
Syracuse, NY, USA
I'm not suggesting you change anything since I doubt I'll ever have to work around the behavior. But I'm still trying to figure it out. Can you give an example where the behavior under discussion has a positive effect?
 
Similar threads
Thread starter Title Forum Replies Date
vefatica TPIPE: unbalanced escaped quotes in a regex? Support 5
A WAD Output redirection to IF block fails with "unbalanced brackets" Support 7
gschizas How to? How can I use select with a folder that already contains parentheses? Support 7
A WAD function return values with parentheses prevent evaluation of additional functions Support 31
D Parsing an array variable to an array does not work Support 5
sully9 How to? Unexpected %DATE% CMD variable format (vs DATE /T format) in CMDebug Support 1
P Inserting string into a variable. Support 3
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
R Difference between TCC and CMD: variable names with hyphens Support 4
Joe Caverly Get a variable type Support 2
M How to set Path variable for non-executable file extensions in 4DOS Support 8
rps For loop internal %_for_files variable? Support 2
A How to safely print variable's value without quotes? Support 9
vefatica Array name in a variable? Support 3
nikbackm TCC 20 variable expansion bug Support 6
X How to set %_INIREAD result to variable Support 5
Charles Dye _USBS variable Support 2
BeechComer Debugger: how to set %1 variable? Support 3
Mordachai Variable followed by function constantly failing... Support 5
T Variable indirection for arrays Support 1
Mike Hall Unexpected "Variable loop" in IF condition Support 10
vefatica If there's no PATH variable ... Support 5
C Maximum length for a variable Support 8
D WAD Internal variable _cpu is undefined Support 2
fpefpe How to? Global variable set /v Support 4
I Change value of a variable while debugging Support 3
I "IF" command in a variable does not expand redirection variable name Support 0
J Variable setting regression in tcc17 ? Support 5
Jay Sage WAD Variable _LOGFILE Not Working Support 2
D Fixed Change in behavior of variable expansion between 16 and 17 Support 3
rps Fixed Using last argument variable ! Support 5
Jay Sage Brackets Around Variable Names Support 11
D Internal variable _tctabs within TCExit Support 9
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
J Set environment variable from stdout of command Support 3
nickles Passing a variable to a command w/o adding it to the local environment Support 7
nickles WAD Variable name gets changed within FOR loop Support 2
nikbackm FUNCTION with variable number of arguments Support 17
rfaquino WAD Apparently a bug when expanding environment variable %ProgramFiles(x86) Support 2
S WAD %_do_errors does not report errors when loop control variable type is not directory entry Support 1
B WAD environment variable in TCC.exe Support 2
Frank watch an array variable in ide/bdebugger Support 2
vefatica Conditional expressions in a variable? Support 2
CWBillow UNSET a Variable Support 7
M Updating environment variable from C++ fails erratically in version 14... Support 57
Frank problem with environment variable x86 vs. x64 Support 2
D WAD Multiple TEE causese weird variable scoping? Support 7
H Variable not expanding with detach Support 3
fpefpe volatile variable Support 5

Similar threads