In bash, I often did things like:
[ -z "${someEnvVar}" ] && someEnvVar="default value"
Instead of creating a full-fledge if statement:
if [ -z "${someEnvVar}" ]; then someEnvVar="default value"; fi
Is there a similar shorthand in PowerShell without creating a function or alias?
I'd rather not write the following in PowerShell:
if ("${someVar}" -eq "") {
$someVar = "default value"
}
If
seems more readable to me, but you can use short circuit of OR (similar to BASH version):
$someVar = ""
[void]($someVar -ne "" -or ($someVar = "Default"))
$someVar #yields "Default"
$someVar = "Custom"
[void]($someVar -ne "" -or ($someVar = "Default"))
$someVar #yields "Custom"
I am afraid Powershell doesnt have such an elegant solution as C# have:
Exp1 ? Exp2: Exp3
msg = (age >= 18) ? "Welcome" : "Sorry";
But you could do something like this:
$someVar = if ("${someVar}" -eq "") { "default value" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With