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?
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.)
Because >
and <
are stream redirection operators in most shells. Well, except that PowerShell doesn't support stream input redirection. That aside, it would be more difficult to parse/interpret >
in some cases to be redirect stdout and in other cases greater than
. Additionally by using the -<operator_name>
approach you can have many more operators than there are intuitive symbols e.g. -contains, -notcontains, -is, -replace, -split, -match, etc. Execute man about_operators
as a starting point to explorer all the operators supported by PowerShell.
The operator = is already the assignment operator. In order not to confuse the comparison and the assignment operator they choose for another operator. In this case for -eq because this is already in use in other (UNIX) scripting languages.
ReferenceURL : https://stackoverflow.com/questions/10289980/why-is-equals-eq-in-powershell-and-not-just
'programing' 카테고리의 다른 글
iOS 6: How do I restrict some views to portrait and allow others to rotate? (0) | 2023.08.21 |
---|---|
Group rows in an associative array of associative arrays by column value and preserve the original first level keys (0) | 2023.08.21 |
AppEngine Java Google+ 로그인에 gplus_id가 누락됨 (0) | 2023.08.21 |
JQuery를 사용한 부트스트랩 탭 활성화 (0) | 2023.08.21 |
개체 배열에서 개체 속성 수정 (0) | 2023.08.21 |