Powershell: Why do I need to escape a double-dash parameter in $args?

后端 未结 1 513
天涯浪人
天涯浪人 2021-01-18 15:08

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

The r

相关标签:
1条回答
  • 2021-01-18 15:17

    -- is considered a special "end-ofparameters" parameter.

    From Bruce Payette's Windows PowerShell in Action:

    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 (--).

    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
    

    Will result in -inputobject as the output.

    0 讨论(0)
提交回复
热议问题