EXIT and & operator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

EXIT and & operator

#1 Post by miskox » 08 Nov 2016 13:44

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: EXIT and & operator

#2 Post by aGerman » 08 Nov 2016 14:09

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

Post Reply