Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does assigning a value to $? crash Powershell? [duplicate]

Tags:

Running the code $? = $true in powershell crashes the program with a "powershell has stopped working" windows error message.

Some builtins like $true or $host throw an error: Cannot overwrite variable true because it is read-only or constant.. Others like $myinvocation or $null are just ignored. Why is $? different? Is there documentation on which builtins have which response? help about_automatic_variables doesn't have any details.

I'm currently running version 5.1. I also tried using powershell -version $n '$? = $true' and versions 3-5 all crash. Interestingly, versions 1 and 2 do not; they throw the read-only variable error.

like image 526
Andrew Avatar asked Dec 20 '16 21:12

Andrew


1 Answers

It looks like this is a known bug. The fix was added to the version that is currently in works (6.0). You can find related pull request here.

My guts tell me that the reason for different behaviour (when compared to other automatic variable) is the fact that both you and system tries to write to this variable virtually at the same time. And even though I don't know why you would want to write to it (I suspect it all started with some if ($? = $true) ...), I do agree that one is really nasty.

like image 65
BartekB Avatar answered Sep 26 '22 16:09

BartekB