How to expand a CMD shell variable twice (recursively)

后端 未结 7 1024
温柔的废话
温柔的废话 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:31

    How to expand a shell variable multiple times:

    @echo off
    setlocal enabledelayedexpansion
    
    set myvar=second
    set second=third
    set third=fourth
    set fourth=fifth
    
    echo Variable value before expansion: !myvar!
    call :expand myvar
    echo Variable value after expansion: !myvar!
    goto :eof
    
    
    :expand
        set var=%1
        :expand_loop
            if not "!%var%!" == "" (
                set var=!%var%!
                goto :expand_loop
            )
            set %1=!var!
        goto :eof
    

    output:

    Variable value before expansion: second
    Variable value after expansion: fifth
    

提交回复
热议问题