Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The result I get is:

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?

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?

like image 945
cdleonard Avatar asked Aug 30 '12 13:08

cdleonard


1 Answers

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

like image 164
ravikanth Avatar answered Oct 12 '22 23:10

ravikanth