The usual advice in batch / command scripting is to use exit /b to exit while setting ERRORLEVEL to indicate an error. However this doesn't play well with CMD's || and && operators. If I run these at the CMD command line:
C:\>echo @exit /b 1 > foo.cmd
C:\>foo.cmd && echo success || echo fail
success
(Expected output is fail).
According to ss64.com this is because "exit code" and ERRORLEVEL aren't always the same, and that exit can return a success (0) exit code when exiting with a non-0 ERRORLEVEL, and exit code is what ||/&&` are paying attention to.
Interestingly, adding call makes it work as I'd expect:
C:\>call foo.cmd && echo success || echo fail
fail
But requiring every use of ||/&& to also use call puts the burden on the consumer, rather than the implementation, of the script.
One example of when this might be useful is if I have a build.cmd script and a test.cmd script, I might want to run build && test.
https://ss64.com/nt/syntax-conditional.html and https://ss64.com/nt/exit.html don't mention this behavior, even though that site is usually very thorough about batch weirdness.
Why is CMD like this? What options exist to avoid this problem?
 
     
     
    