Powershell -eq 运算符

Powershell -eq operator

为什么表达式 "1" -eq 1 在 powershell 中变为 TRUE?第一个是string类型,第二个是integer类型,为什么要等同呢

这称为隐式类型转换。

当在需要不同类型的上下文中使用一种类型的值时,Powershell 会自动执行类型转换。

A type conversion is performed when a value of one type is used in a context that requires a different type. If such a conversion happens automatically it is known as implicit conversion. (A common example of this is with some operators that need to convert one or more of the values designated by their operands.) Implicit conversion is permitted provided the sense of the source value is preserved, such as no loss of precision of a number when it is converted.

source

除了官方文档,这里还有 mklement0 here with some additional examples and references:

的精彩回答