PowerShell 变量“${$}”、“${^}”和“$?”

PowerShell Variables `${$}`, `${^}` and `$?`

有谁知道以下变量在 PowerShell 中的用途:

据我所知,${^}${$} 都与上次执行的命令行有关(如果您通过 ISE 运行将它们作为脚本使用,它们与在脚本之前执行的命令是 运行,而不是同一脚本中的前一行)。区别似乎是 ${^} return 是第一个空白字符之前的命令,而 ${$} return 是最后一个空白字符之后的所有内容。即

$? 同时似乎总是 return true.

我在 ISE 的自动完成功能中发现了这些变量。

我确信这已记录在案,但我一直在努力寻找正确的搜索词来找到答案/除了噪音之外的任何东西。

$?:

$?包含最后一个操作的执行状态。相当于 CMD shell 中的 %errorlevel%。另请参阅下面的 $LastExitCode。 如果上次操作成功则它包含 TRUE,如果失败则包含 FALSE。只读,AllScope。 (https://ss64.com/ps/syntax-automatic-variables.html)

其余的也在那里记录。

谷歌搜索角色名称而不是角色本身有效!

http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

${^}也可以写成$^。这给出了最后一个命令的第一个标记。这类似于我在问题中所说的,只有一个标记可以包含空格;而是根据解析的代码拆分事物。举例说明:'number 1', 'number 2' | Write-Host 会 return number 1 而不是 'number.

${$} / $$ 同样 return 是最后一个标记。即在 运行 write-host -ForegroundColor green -Object 'hello, is it me you''re looking for?'$$ 之后给出 hello, is it me you're looking for?.

$? return如果上一个命令成功则为真。为了证明它在哪里是错误的,运行 1/0 然后 $? 将给出一个错误的结果。

$$

Contains the last token in the last line received by the current session.

$?

Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$^

Contains the first token in the last line received by the session.