question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The Conditional If does not produce error if specific syntax is incorrect

See original GitHub issue

Steps to reproduce:

Using the conditional If, use the incorrect comparison operator of = instead of -eq:

$Sky = 'blue'

if ($Sky = 'green') {

    Write-Host "The sky is green"
}

Expected behavior

We should get the error Unexpected token '=' in expression or statement like this if we used the incorrect comparison operator equals:

ParserError: 
Line |
   3 |  if ($Sky equals 'green') {
     |           ~~~~~~
     | Unexpected token 'equals' in expression or statement.

The variable should also not have been changed to green as you don’t set variables inside of the condition to be met inside of conditionals.

Actual behavior

No error is produced, the script continues, and the variable is set to green


## Environment data

Observed in both PowerShell & Windows PowerShell

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vexx32commented, Aug 30, 2020

This is by design.

What’s happening here is that the expression $sky = 'green', which doesn’t normally output data, can also be written ($sky = 'green'), which does produce output (it outputs the value 'green' to the pipeline as well as storing it in the variable).

Knowing this, the parentheses in an if statement (and indeed other conditionals like switch) behave the same way, forcing the output and processing it in the conditional statement.

C# works the same way, it’s likely a pattern copied from there.

$sky = 'green' # no output
($sky = 'blue') # outputs 'blue'
if ($sky = 'green') {
    # 'green' is evaluated by the if statement and coerces to a boolean `$true` as it is not an empty or null string.
    Write-Warning "we do indeed get here"
}

$sky # is now 'green'

In other words, assigning a variable in an if statement will always be an assignment and a test of the value you assigned, and never a test of the current value of the variable.

0reactions
user8446commented, Aug 30, 2020

Thank you @vexx32

Read more comments on GitHub >

github_iconTop Results From Across the Web

Makefile conditional error
The shell doesn't understand ifeq ; that's a make construct. You'll have to use shell if-statements inside a recipe. And, you don't have...
Read more >
if / else errors - learn how to fix these
Have a look and make sure you don't have any semicolons on your `else` or ... Typically the following error messages are caused...
Read more >
Why is there a syntax error if I don't write 'if' in an END block ...
AWK programs are a series of rules, and possibly functions. ... be specified as part of an if statement (or other conditional statement, ......
Read more >
Using IF with AND, OR and NOT functions
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and...
Read more >
Conditionals with if/else & Booleans | AP CSP (article)
The condition is a Boolean expression: an expression that evaluates to either true or false . Boolean values are another type of data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found