Windows PowerShell 中的 Bash ` (backquote/backtick) 是什么?

What is the equivalent of the Bash ` (backquote/backtick) in Windows PowerShell?

我需要一个命令的结果作为另一个命令的参数。

command /opt <another command output>

在BashLinux我会

command /opt `another command`

Windows 终端中的 ` 符号等效于什么?

特别是,我正在使用命令

'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}

其中 findstr 是等同于 grep 的 windows,管道中的最终命令等同于 sed。此命令的输出将是 1、2 或 3,我需要将其传递给下一行中的 %HERE

'C:\Program Files\Display\display64.exe' /device %HERE /rotate 90

以下最小示例不起作用:

FOR /F %a in ('"C:\Program Files\Display\display64.exe"  /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}') do echo "%a"

我做错了什么? (我是 Windows 的新人)。

使用 (...)grouping operator 将一个命令的输出作为参数传递给另一个命令:

'C:\Program Files\Display\display64.exe' /device (
  'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}
) /rotate 90

注:$(...),详情见subexpression operator, works too, but (...) is usually sufficient and has no side effects; you only need $(...) for multiple (;-separated) statements - see


至于你试过的

  • 在POSIX-compatible shell中,例如Bash,`...`命令替换的遗留形式 其现代语法是 $(...) - 虽然 PowerShell 也支持 $(...),但 (...) 通常更可取,如前所述。

  • 在 PowerShell 中,` 用作 转义字符 (仅),类似于 POSIX-compatible 中的 \ shells - 请参阅概念性 about_Special_Characters 帮助主题。

  • 您尝试使用 FOR /F %a in ...%HERE(应该是 %HERE%)与 遗留 cmd.exe shell Windows,而不是它的继任者 PowerShell,其语法根本不同。