Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell equivalent to bash's set -u

Normally, bash replaces an undefined variable with an empty string:

$ echo "$BLAH things after blah"
 things after blah

But there's a handy way to change this behavior:

$ set -u
$ echo "$BLAH things after blah"
sh.exe": BLAH: unbound variable

(Pardon the exe. I'm running under MSYS bash.) I put this at the top of all my bash scripts. (Fail fast being a Good Thing and all that.)

PowerShell normally behaves the same way as bash:

PS C:\> echo "$blah things after blah"
 things after blah

My question: Is there any thing in PowerShell that changes this behavior so it will cause an error when I attempt to use an uninitialized variable?

like image 403
jpmc26 Avatar asked Sep 03 '25 03:09

jpmc26


1 Answers

This should do the trick:

set-strictmode -version 2.0

http://technet.microsoft.com/en-us/library/hh849692.aspx

like image 200
Apothis Avatar answered Sep 05 '25 00:09

Apothis