In PowerShell, why does this return nothing:
PS > $y = "$prid?api"
PS > echo $y
But these work better:
PS > $y = "$prid\?api"
PS > echo $y
18\?api
PS > $y = "${prid}?api"
PS > echo $y
18?api
In PowerShell, why does this return nothing:
PS > $y = "$prid?api"
PS > echo $y
But these work better:
PS > $y = "$prid\?api"
PS > echo $y
18\?api
PS > $y = "${prid}?api"
PS > echo $y
18?api
Surprisingly, ? can be used in a PowerShell variable name without requiring that the name be enclosed in {...}.
Therefore, based on the rules of PowerShell's expandable strings (string interpolation inside "..." strings), "$prid?api" looks for a variable with verbatim name prid?api rather than considering the ? to implicitly terminate the preceding identifier.
That is, you can actually define and reference a variable named prid?api as follows:
PS> $prid?api = 'hi'; $prid?api
hi
This surprising permissiveness actually gets in the way of a recently introduced language feature, null-conditional access, introduced in PowerShell (Core) 7.1:
# v7.1+; note that the {...} around the name is - unexpectedly - *required*.
${variableThatMayBeNull}?.Foo()
GitHub issue #14025 advocates for obviating the need to use {...} in this case.