Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is equals "-eq" in PowerShell and not just "="?

The constructs -ne, -eq, and -gt looks at least strange.

if ($true -eq $true){} 

but not

if ($true = $true){} 

What is the explanation?

like image 948
Mike Chaliy Avatar asked Apr 23 '12 23:04

Mike Chaliy


People also ask

Why does PowerShell use EQ?

PowerShell -EQ and -CEQ If you ever need to see if an object is equal to another object you have to use the eq (case-insensitive) or ceq (case sensitive) operators. These operators test the value of each entity you'd like to compare against.

What is equal to in PowerShell?

PowerShell doesn't use an equals sign (=) to test equality because it's used for the assignment operator. Similarly, PowerShell doesn't use the greater than (>) or less than (<) characters because they're used for output and input redirection, respectively.

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

Basically the answer is that it is how Unix has done it forever. Sure enough, if you write some Bash scripts that's what you'll use and it's actually nice to have your PowerShell syntax knowledge transfer over one for one to Bash.

It is answered in detail in Bruce Payette's Windows PowerShell in Action, Second Edition (Kindle Location 3391).

Let’s talk about the most contentious design decision in the PowerShell language.

And the winner is: why the heck didn’t we use the conventional symbols for comparison like >, >=, <, <=, ==, and !=?

The answer is that the > and < characters are used for output redirection. Because PowerShell is a shell and all shell languages in the last 30 years have used > and < for I/O redirection, people expected that PowerShell should do the same. During the first public beta of PowerShell, this topic generated discussions that went on for months.

We looked at a variety of alternatives, such as modal parsing where sometimes > meant greater-than and sometimes it meant redirection. We looked at alternative character sequences for the operators like :> or ->, either for redirection or comparison. We did usability tests and held focus groups, and in the end, settled on what we had started with.

The redirection operators are > and <, and the comparison operators are taken from the Unix test(1) command. We expect that, because these operators have a 30-year pedigree, they’re adequate and appropriate to use in PowerShell. (We also expect that people will continue to complain about this decision, though hopefully not for 30 more years.)

like image 144
Andy Arismendi Avatar answered Sep 28 '22 05:09

Andy Arismendi