1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unbalanced parentheses in variable expansion

Discussion in 'Support' started by Charles Dye, Aug 1, 2012.

  1. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    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.
     
  2. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    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)
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    It seems wrong to me.
    Code:
    v:\> set a=(
    v:\> echo %a
    (
    v:\> echo %a %a
    ( %a
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    It goes way back, though. Even if it is a bug, it may be unfixable without causing some kind of compatibility issues :-(
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    One might say it already is a (CMD) compatibility issue.
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    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.
     
  7. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    It would be, if CMD supported nested variable expansion. (It doesn't.)

    If you want CMD compatibility, use SETDOS /X-4.
     
  8. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    Okay; thanks. I probably never would have thought to try that one.
     
  9. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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?
     
  10. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    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.)
     
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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
     
  12. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Because it's not expanded & detected prior to nested variable expansion. Your ( is a literal.
     
  13. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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?
     

Share This Page