Page 1 of 1

EXIT and & operator

Posted: 08 Nov 2016 13:44
by miskox
Let's have two .cmd procedures:

a.cmd:

Code: Select all

@echo off
echo A
exit


And b.cmd:

Code: Select all

@echo off
echo B


Now execute a.cmd and then b.cmd:

Code: Select all

c:\a.cmd&b.cmd


Result:

CMD window closes.

Now change a.cmd to this:

Code: Select all

@echo off
echo A
goto :EOF


(goto :EOF above could of course be omitted in this case).

Execute it:

Code: Select all

c:\a.cmd&b.cmd


Result:

Code: Select all

c:\>a.cmd&b.cmd
A
B

c:\>


We can see that b.cmd is not executed if we have EXIT in procedure a.cmd. I would expect: a.cmd completes with EXIT and next command (calling of b.cmd) executes. But looks like it is not.

Can this be avoided? Thoughts?

Thanks.
Saso

Re: EXIT and & operator

Posted: 08 Nov 2016 14:09
by aGerman
It's the expected and documented behavior.
http://www.dostips.com/DosCommandIndex.php#EXIT

EXIT is counterproductive in almost every case because it quits the cmd.exe process.

If you need to return a value use EXIT /B n (with n an integral 32 bit value). If not, use either EXIT /B or GOTO :EOF. All of them do only Exit the current batch script.

Steffen