Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(I am using PowerShell v2.)</p> <p>The '<code>$?</code>' variable is documented in <code>about_Automatic_Variables</code>:</p> <pre> $? Contains the execution status of the last operation </pre> <p>This is referring to the most recent PowerShell operation, as opposed to the last external command, which is what you get in <code>$LastExitCode</code>.</p> <p>In your example, <code>$LastExitCode</code> is 0, because the last external command was <code>cmd</code>, which was <em>successful</em> in echoing some text. But the <code>2&gt;&amp;1</code> causes messages to <code>stderr</code> to be converted to error records in the output stream, which tells PowerShell that there was an error during the last <em>operation</em>, causing <code>$?</code> to be <code>False</code>.</p> <p>To illustrate this a bit more, consider this:</p> <pre> > java -jar foo; $?; $LastExitCode Unable to access jarfile foo False 1 </pre> <p><code>$LastExitCode</code> is 1, because that was the exit code of java.exe. <code>$?</code> is False, because the very last thing the shell did failed.</p> <p>But if all I do is switch them around:</p> <pre> > java -jar foo; $LastExitCode; $? Unable to access jarfile foo 1 True </pre> <p>... then <code>$?</code> is True, because the last thing the shell did was print <code>$LastExitCode</code> to the host, which was successful.</p> <p>Finally:</p> <pre> > &{ java -jar foo }; $?; $LastExitCode Unable to access jarfile foo True 1 </pre> <p>...which seems a bit counter-intuitive, but <code>$?</code> is <em>True</em> now, because the execution of the script block was <em>successful</em>, even if the command run inside of it was not.</p> <hr> <p>Returning to the <code>2&gt;&amp;1</code> redirect.... that causes an error record to go in the output stream, which is what gives that long-winded blob about the <code>NativeCommandError</code>. The shell is dumping the whole error record.</p> <p>This can be especially annoying when all you want to do is pipe <code>stderr</code> <em>and</em> <code>stdout</code> together so they can be combined in a log file or something. Who wants PowerShell butting in to their log file??? If I do <code>ant build 2&gt;&amp;1 &gt;build.log</code>, then any errors that go to <code>stderr</code> have PowerShell's <em>nosey</em> $0.02 tacked on, instead of getting clean error messages in my log file.</p> <p>But, the output stream is not a <em>text</em> stream! Redirects are just another syntax for the <em>object</em> pipeline. The error records are objects, so all you have to do is convert the objects on that stream to <em>strings</em> before redirecting:</p> <p>From:</p> <pre> > cmd /c "echo Hello from standard error 1>&2" 2>&1 cmd.exe : Hello from standard error At line:1 char:4 + cmd &2" 2>&1 + CategoryInfo : NotSpecified: (Hello from standard error :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError </pre> <p>To:</p> <pre> > cmd /c "echo Hello from standard error 1>&2" 2>&1 | %{ "$_" } Hello from standard error </pre> <p>...and with a redirect to a file:</p> <pre> > cmd /c "echo Hello from standard error 1>&2" 2>&1 | %{ "$_" } | tee out.txt Hello from standard error </pre> <p>...or just:</p> <pre> > cmd /c "echo Hello from standard error 1>&2" 2>&1 | %{ "$_" } >out.txt </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload