Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

syntax - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...