Recently I discovered some nasty behavior about assigning some version-like value to variable in Powershell (at least in 7.2.5).
At first I tried:
> $version = 1.2.3
> echo $version
I quickly found out that I have to escape thr value with quotes to make it string explicitly. This one works well:
> $version = "1.2.3"
> echo $version
1.2.3
The question is: Why does it work this way? Why does pwsh
not throw some kind of error in the first example and just converts it to $null
? Like in other cases:
> 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 | [int]12a
| ~~~
| Unexpected token '12a' in expression or statement.
So I believe literals like 1.2.3
are expected and managed by some rule.
I tried to dig into documentation about expressions and assignments, but can't find the correct answer.