Powershell:为什么我需要转义$ args中的double-dash参数? [英] Powershell: Why do I need to escape a double-dash parameter in $args?
问题描述
以下是一个非常简单的示例脚本:
Here's a very simple example script:
function fun () { "AAA $args ZZZ" }
fun a b c
fun a - b
fun a -- b
fun a '--' b
fun a --- b
我得到的结果是:
AAA a b c ZZZ
AAA a - b ZZZ
AAA a b ZZZ
AAA a -- ZZZ
AAA a --- b ZZZ
我显然需要逃避双破折号。为什么?
I apparently need to escape the double dash. Why?
我为一套脚本编写了一个powershell封装,其中一些脚本为 - 分配了一个特定的含义。如何通过未修改的方式传递?
I'm writing a powershell wrapper for a suite of scripts and some of those scripts assign a specific meaning to "--". How can I pass it through unmodified?
推荐答案
- 被认为是一个特殊的end-of- parameters参数。
-- is considered a special "end-ofparameters" parameter.
来自Bruce Payette的Windows PowerShell操作:
From Bruce Payette's Windows PowerShell in Action:
引号使参数binder不处理引用的字符串参数。
另一个不常用的方法是使用特殊的end-of-parameters
参数,这是两个连字符背对背( - )。
The quotes keep the parameter binder from treating the quoted string as a parameter. Another, less frequently used way of doing this is by using the special "end-ofparameters" parameter, which is two hyphens back to back (--).
此序列之后的所有内容都将被视为参数,即使它看起来像一个参数。例如,使用 - 你也可以不使用引号写出-InputObject字符串:
Everything after this sequence will be treated as an argument, even if it looks like a parameter. For example, using -- you can also write out the string -InputObject without using quotes:
PS (3) > Write-Output -- -InputObject
将输出-inputobject作为输出。
Will result in -inputobject as the output.
这篇关于Powershell:为什么我需要转义$ args中的double-dash参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!