1

What's the Windows batch file equivalent of set -o errexit in a bash script?

I have a long batch file filled with different programs to run on Windows command line... basically its an unrolled make file with every compiler command that needs to be run to build an exe in a long sequence of commands.

The problem with this method is that I want it to exit the batch command on the first non-zero return code generate by a command in the script.

As far as I know, Windows batch files have a problem where they don't automatically exit on the first error without adding a lot of repetitive boilerplate code between each command to check for a non-zero return code and to exit the script.

What I'm wondering about, is there an option similar to bash's set -o errexit for Windows cmd.exe? or perhaps a technique that works to eliminate too much boilerplate error checking code... like you set it up once and then it automatically exits if a command returns a non-zero return code without adding a bunch of junk to your script to do this for you.

(I would accept PowerShell option as well instead of cmd.exe, except PowerShell isn't very nice with old-unix-style command flags like: -dontbreak -y ... breaking those commands without adding junk to your command line like quotes or escape characters... not really something I want to mess around with either...)

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Bimo
  • 5,987
  • 2
  • 39
  • 61
  • 1
    AFAIK, there is no equivalent. Batch files test ERRORLEVEL and exit if it's not an acceptable value. See [What is the Windows/cmd.exe equivalent of Linux/bash's $?](https://stackoverflow.com/q/277288/62576) – Ken White Jul 25 '19 at 00:14

1 Answers1

3

CMD/Batch

As Ken mentioned in the comments, CMD does not have an equivalent to the bash option -e (or the equivalent -o errexit). You'd have to check the exit status of each command, which is stored in the variable %errorlevel% (equivalent to $? in bash). Something like

if %errorlevel% neq 0 then exit /b %errorlevel%

PowerShell

PowerShell already automatically terminates script execution on errors in most cases. However, there are two error classes in PowerShell: terminating and non-terminating. The latter just displays an error without terminating script execution. The behavior can be controlled via the variable $ErrorActionPreference:

  • $ErrorActionPreference = 'Stop': terminate on all errors (terminating and non-terminating)
  • $ErrorActionPreference = 'Continue' (default): terminate on terminating errors, continue on non-terminating errors
  • $ErrorActionPreference = 'SilentlyContinue': don't terminate on any error

PowerShell also allows more fine-grained error handling via try/catch statements:

try {
    # run command here
} catch [System.SomeException] {
    # handle exception of a specific type
} catch [System.OtherException] {
    # handle exception of a different type
} catch {
    # handle all other exceptions
} finally {
    # cleanup statements that are run regardless of whether or not
    # an exception was thrown
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328