小编Dmi*_*hev的帖子

在 Powershell 中分配类似版本的值(带有两个或更多点的数字)

最近我发现了一些关于在 Powershell 中将一些类似版本的值分配给变量的令人讨厌的行为(至少在 7.2.5 中)。

起初我尝试过:

> $version = 1.2.3
> echo $version

Run Code Online (Sandbox Code Playgroud)

我很快发现我必须用引号转义 thr 值才能显式地使其成为字符串。这个效果很好:

> $version = "1.2.3"
> echo $version
1.2.3
Run Code Online (Sandbox Code Playgroud)

问题是:为什么会这样?为什么pwsh在第一个示例中不抛出某种错误而只是将其转换为$null?就像其他情况一样:

> 12a
12a: The term '12a' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

> [int]12a
ParserError:
Line |
   1 | …
Run Code Online (Sandbox Code Playgroud)

powershell type-conversion powershell-core

4
推荐指数
1
解决办法
290
查看次数