Why does calling a nested batch file without prepending “call” to the line exit the parent batch file?

前端 未结 3 1529
挽巷
挽巷 2020-12-17 07:45

I understand how to call nested batch files from within a parent file using the call command, as there are plenty of resources on that:

  • CALL
3条回答
  •  星月不相逢
    2020-12-17 08:33

    As Sean Cheshire wrote, it's necessary for backward compatibility.

    But starting a batch file from a batch file without using CALL does not terminate the parent!
    It looks that way, as the parent normally will not executed further after the second batch exits.
    But using a call before starting the second.bat, will show that the first batch isn't terminated.

    parent.bat

    echo parent.bat
    call :myLabel
    echo back in parent.bat main
    exit /b
    
    :myLabel
    second.bat & echo back in parent.bat
    exit /b
    

    second.bat

    echo second.bat
    exit /b
    

    I use here the the secpond.bat & echo back ... to avoid another bug/feature of cmd.exe.
    If you use second.bat without any extras it will start second.bat AND jump to the label :myLabel in second.bat!

提交回复
热议问题