什么是 cmd/PowerShell 相当于 Bash 上的回勾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/434038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:53:43  来源:igfitidea点击:

What's the cmd/PowerShell equivalent of back tick on Bash?

bashpowershellcmd

提问by J?rg W Mittag

Redirecting command output:

重定向命令输出:

For example:

例如:

echo "Foo `./print_5_As.rb`"

would echo "Foo AAAAA"

会回声“Foo AAAAA”

回答by J?rg W Mittag

The PowerShell syntax is based on the POSIX ksh syntax (and interestingly noton any of Microsoft's languages like CMD.EXE, VBScript or Visual Basic for Applications), so many things work pretty much the same as in Bash. In your case, command substitution is done with

PowerShell 语法基于 POSIX ksh 语法(有趣的是,它基于任何 Microsoft 语言,如 CMD.EXE、VBScript 或 Visual Basic for Applications),因此很多东西的工作方式与 Bash 中的几乎相同。在您的情况下,命令替换完成

echo "Foo $(./print_5_As.rb)"

in both PowerShell and Bash.

在 PowerShell 和 Bash 中。

Bash still supports the ancient way (backticks), but PowerShell cleaned up the syntax and removed redundant constructs such as the two different command substitution syntaxes. This frees up the backtick for a differentuse in PowerShell: in POSIX ksh, the backslash is used as escape character, but that would be verypainful in PowerShell because the backslash is the traditional path component separator in Windows. So, PowerShell uses the (now unused) backtick for escaping.

Bash 仍然支持古老的方式(反引号),但 PowerShell 清理了语法并删除了冗余结构,例如两种不同的命令替换语法。这释放了 反斜杠在 PowerShell 中的不同用途:在 POSIX ksh 中,反斜杠用作转义字符,但这在 PowerShell中会非常痛苦,因为反斜杠是 Windows 中的传统路径组件分隔符。因此,PowerShell 使用(现在未使用的)反引号进行转义。

回答by matli

In CMD.EXE there is no direct equivalent. But you can use the FOR command to achieve what you want.

在 CMD.EXE 中没有直接的等价物。但是您可以使用 FOR 命令来实现您想要的。

Do something like the following:

执行以下操作:

FOR /F "usebackq" %x IN (`./print_5_As.rb`) DO @echo Foo %x

or

或者

FOR /F %x IN ('"./print_5_As.rb"') DO @echo Foo %x

You might need to set delimiter to something else than the default, depending on how the output looks and how you want to use it. More details available in the FOR documentation at https://technet.microsoft.com/en-us/library/bb490909.aspx

您可能需要将分隔符设置为默认值以外的其他内容,具体取决于输出的外观以及您希望如何使用它。在https://technet.microsoft.com/en-us/library/bb490909.aspx的 FOR 文档中提供了更多详细信息

回答by Pete Richardson

In PowerShell, you use $( ) to evaluate subexpressions...

在 PowerShell 中,您使用 $( ) 来计算子表达式...

For example:

例如:

PS C:\> "Foo $(./print_5_As.rb)"
Foo AAAAA