为什么不需要从管道中涉及的被调用批处理脚本调用 return?

Why is there no need for `call` to return from called batch script which is involved in a pipe?

假设有一个批处理文件(调用者)执行另一个批处理文件(被调用者),需要使用call命令以便在被调用者执行完成后return给调用者.这是一个例子:

caller.bat:

echo Calling another script...
call callee.bat
echo Returned from callee...

callee.bat(在同一位置):

echo   Being called from caller...

输出将是这样的(省略命令回显),表明执行 returned 符合预期:

Calling another script...
  Being called from caller...
Returned from callee...

如果调用者取消了 call 命令,输出将是:

Calling another script...
  Being called from caller...

但是一旦被调用者参与管道(|),是否使用call命令就没有区别了。例如:

caller.bat(被调用者保持不变):

echo Calling another script...
break | callee.bat
echo Returned from callee...

虽然没有 call 命令,但输出将是这样。

Calling another script...
  Being called from caller...
Returned from callee...

这种行为的原因是什么,是什么导致执行到这里的调用者return?

两种方法从调用方调用另一个批处理文件(main 文件):call callee.batcmd /C callee.bat;不同之处在于 call 在调用程序的相同上下文 中执行另一个批处理文件 ,因此它们共享相同的环境变量和另一个状态,而 cmd /C在完全独立的上下文中执行另一个批处理文件。作为个人笔记,我曾经将通过 call 调用的批处理文件命名为 internal subroutine,将通过 external suroutine 调用的批处理文件命名为cmd /C(和 overlay 直接调用批处理文件而没有 call 也没有 cmd /C,即 继承了 行为和调用者批处理文件的上下文)。

在管道的执行中,管道的两边都是通过一个cmd /C执行的,所以两边都是作为外部子程序调用的。这样,如果管道的任何一侧是 Batch.BAT 文件,它在结束时 returns 到调用程序。

同样的行为发生在放置在 for /F 命令中的被调用者批处理文件中,出于同样的原因,exaclty; for /F %%a in ('calle.bat') do ...