Suppose I have batch file one.cmd
:
@two.cmd && echo success || echo fail
and two.cmd
:
@exit /b 1
If I run this at an interactive CMD prompt it behaves one way:
D:\>one.cmd
success
But if I run it under cmd /c
it behaves a different way:
D:\>cmd /c one.cmd
fail
Why is this?
Some other cases
PowerShell and Python's subprocess.run
both give the same result as cmd /c
.
cmd /k
gives the same result as running interactively. Which I guess makes sense, given that it's a new interactive shell.
Why this matters
I know how to fix this. Either (or both) of:
- any use of
||
and&&
should be paired withcall
. - always exit batch files with
cmd.exe /c exit %ERRORLEVEL%
.
See more here: Why doesn't Windows batch file `exit` work with `||`?).
My real goal is to write a unit test to ensure that my batch file correctly propagates error on failure, but because the unit test is not using an interactive shell, it doesn't reproduce the problem.