Why is there no need to call `call` from the called script package that is involved in the pipe?

Suppose that there is a batch file (caller) that executes another batch file (called), the call command should be used to return to the caller after the call is completed. Here is an example:

caller.bat :

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

callee.bat (in the same place) :

 echo Being called from caller... 

The output will be like this (excluding echo commands), showing that the execution returned as expected:

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

If the call command was fired at the caller, the output will be:

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

But once the called object is involved in the pipe ( | ), there is no difference if the call command is used. For instance:

caller.bat (called remains unchanged) :

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

The output will be this, although there is no call command.

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

What is the reason for this behavior that causes execution to return to the caller here?

+6
source share
1 answer

There are two ways to call another batch file from the calling (main file): call callee.bat and cmd /C callee.bat ; the difference is that call executes another batch file in the same context of the calling program, so they use the same environment variables and different status, while cmd /C executes another batch file in a completely separate context. As well as a personal note, I used the name of the batch file to be called via call for the internal routine, and the external routine that was called via cmd /C (and overlaid the batch file directly called without call and cmd /C , which inherits the behavior and the context of the caller's batch file).

When executing a pipe, both sides of the channel are executed via cmd /C , so both sides are called as external routines. Thus, if either side of the channel is a BAT file, it returns to the caller when it ends.

The same behavior occurs in the callee batch file placed in the for /F command and exaccty for the same reason; for /F %%a in ('calle.bat') do ...

+6
source

All Articles