How to expand a CMD shell variable twice (recursively)

后端 未结 7 1027
温柔的废话
温柔的废话 2020-12-01 13:03

Using the Windows XP CMD command-line I can expand a variable twice as follows:

set AAA=BBB
set BBB=CCC
for /F \"usebackq tokens=*\" %i in (`echo %%AAA%%`) d         


        
7条回答
  •  天涯浪人
    2020-12-01 13:35

    The OP question almost works on Windows 10. A small correction is required to properly form the ECHO command. Here is the intermediate result:

    set LIST=BBB CCC DDD
    set BBB=111
    set CCC=222
    set DDD=333
    for %%i in (%LIST%) do echo %%%%i%%
    
    rem // Outputs:
    rem // %AAA%
    rem // %BBB%
    rem // %CCC%
    

    Then we nest the output inside another FOR statement and get the completed result:

    set LIST=BBB CCC DDD
    set BBB=111
    set CCC=222
    set DDD=333
    for %%i in (%LIST%) do for /f %%a in ('echo %%%%i%%') do echo %%a
    
    rem // Outputs:
    rem // 111
    rem // 222
    rem // 333
    

提交回复
热议问题