I have some Batch scripts I use for automating application build processes, most of which involve chaining commands together using the &&
operator. Admittedly, I'm more experienced with Linux, but based on that experience some_command && other_command
should result in other_command
being run iff some_command
returns an exit code of 0. This answer and this answer seem to agree with that. However this appears not to be the case on Windows cmd.exe, all of the scripts run regardless of the error code of the previous.
I decided to make a simple test for this to convince myself I wasn't going insane. Consider this test.bat
, which returns an exit code of 1:
@echo off
EXIT /B 1
Running test.bat && echo This shouldn't print
prints 'This shouldn't print'. But since the exit code is clearly 1, echo
should not be called. I've tested that the error code was actually 1 using the %errorlevel%
variable, they're coming out as expected (0 before I run the script, 1 after).
On Linux I tried the same thing. Here's test.sh
:
#!/bin/bash
exit 1
Running ./test.sh && echo "This shouldn't print"
gives no output, exactly what I expected.
What's going on here?
(Note: OS is Windows 7 Enterprise)