Skip to content

Redirected stderr output causes $? to be set to $false, even if the process exit code is 0 #13393

Closed

Description

PR #13361 missed one aspect of what is was trying to fix, namely what was originally reported in #10512:

How $? is set in an external-program call should solely depend on the process exit code, irrespective of whether there is stderr output and whether or not stderr output is redirected.

It is the latter that's still a problem, which matters primarily with respect to && and ||:

# On Unix
PS> sh -c 'ls nosuch; :' 2>$null || 'why did I get here?'
why did I get here?

Steps to reproduce

# On Unix
sh -c 'ls nosuch; :' 2>$null; $? | Should -BeTrue

Expected behavior

The test should succeed:

Since the exit code of the sh command is 0, $? should be $true - irrespective of the presence of stderr output, whether redirected or not.

Actual behavior

The test fails, because the presence of redirected stderr output unexpectedly set $? to `$false

Expected $true, but got $false.

Environment data

PowerShell Core 7.1.0-preview.5

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Aug 10, 2020
  2. changed the title [-]My bug report[/-] [+]Redirected stderr output causes $? to be set to $false, even if the process exit code is 0[/+] on Aug 10, 2020
  3. SteveL-MSFT commented on Aug 10, 2020

    @SteveL-MSFT
    Member

    Since @PowerShell/powershell-committee agreed that stderr is not an indicator of an error, it makes sense to not set $? to false.

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

Metadata

Metadata

Labels

Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions