Skip to content

Equivalent of bash set -e #3415

Closed
Closed

Description

I asked this question on twitter and it was suggested I post here.

Many people consider it good practice to start a bash script.

#! /bin/bash
set -eu

set -e tells the script to stop on first error
set -u tells the script to not allow undeclared variables

Although not everyone agrees

I think set -u and Set-StrictMode -Version 1.0 are equivalent.

I just wrote a set of Powershell scripts for managing an elasticsearch server. Then a few top-level scripts to tie them together.

It would have been really useful to be able to write something equivalent to...

set -e

$someValue = .\server-task-1.ps1
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1

... but I couldn't find how to and ended up with something more like...


$someValue = .\server-task-1.ps1
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

.\server-task-2.ps1  -using $someValue
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

.\task-that-requires-everything-else-suceeded.ps1
if ($LastExitCode -ne 0) {
    exit $LastExitCode 
}

Apologies if this is already possible and I've missed it...

Activity

  1. SteveL-MSFT commented on Mar 25, 2017

    @SteveL-MSFT
    Member

    $erroractionpreference = "stop" is similar to set -e, except it only works against cmdlets and not native commands

  2. added
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Mar 25, 2017
  3. pauldambra commented on Mar 27, 2017

    @pauldambra
    Author

    As someone fresh to Powershell that's a confusing distinction.

    So would $erroractionpreference = "stop" work for my example above..?

    i.e.

    $erroractionpreference = "stop"
    
    $someValue = .\server-task-1.ps1
    
    # does not run the next two commands if server-task-1.ps1 exits with code != 0
    .\server-task-2.ps1 -using $someValue
    .\task-that-requires-everything-else-suceeded.ps1
    

    but

    $erroractionpreference = "stop"
    
    $someValue = .\server-task-1.exe
    
    # does run the next two commands _even though_ server-task-1.exe exits with code != 0
    .\server-task-2.ps1 -using $someValue
    .\task-that-requires-everything-else-suceeded.ps1
    
  4. SteveL-MSFT commented on Mar 27, 2017

    @SteveL-MSFT
    Member

    $erroractionpreference = "stop" only works if Write-Error is called (such as an exception is raised). It doesn't do any checks against $lastexitcode. We can certainly look into adding another preference variable to throw an exception if $lastexitcode != 0.

  5. mopadden commented on Mar 29, 2017

    @mopadden

    Would probably want to throw the exception only in a context where Powershell also sets $? to false as a result.

    Throwing an exception in other situations where lastexitcode != 0 would not give a result equivalent to "set -e" in bash.

    https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
    Edit: the idea is to throw an error on failure where the success/failure of an application is not tested.

  6. added this to the milestone on Mar 29, 2017
  7. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    WG-Enginecore PowerShell engine, interpreter, and runtime
    WG-Languageparser, language semantics
    and removed
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    WG-Enginecore PowerShell engine, interpreter, and runtime
    on Mar 29, 2017
  8. mopadden commented on Apr 7, 2017

    @mopadden

    Hi, i've done some work on this, can I ask for an assignment?

  9. SteveL-MSFT commented on Apr 11, 2017

    @SteveL-MSFT
    Member

    mopadden consider it assigned, GitHub doesn't allow assigning to people without write access

  10. 7 remaining items

  11. b-rad15 commented on Jun 29, 2021

    @b-rad15

    Since this is the first search result, linking the most recent thread on this RFC that I can find for anyone interested in implementing it PowerShell/PowerShell-RFC#277

  12. daxian-dbw commented on Nov 10, 2021

    @daxian-dbw
    Member

    The PR that implements the RFC was already merged as an experimental feature: #15897
    So, closing this issue.

  13. nhooyr commented on Dec 16, 2022

    @nhooyr

    Is the equivalent of set -eu then:

    Enable-ExperimentalFeature PSNativeCommandErrorActionPreference
    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"
    $PSNativeCommandUseErrorActionPreference = $true
  14. LinAGKar commented on Apr 8, 2025

    @LinAGKar

    It seems like $ErrorActionPreference = "Stop" makes it exit with 0 (success) on error, so it's not equivalent to set -e in that regard either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions