Why does PowerShell separate arguments containing hyphens and periods?

In the PowerShell window:

PS C:\> echo -abc.def.ghi -abc .def.ghi 

For some reason, a combination of a hyphen and a period causes Powershell to split the argument into two lines.

This does not happen without a hyphen:

 PS C:\> echo abc.def.ghi abc.def.ghi 

And this does not happen when there are no periods:

 PS C:\> echo -abcdefghi -abcdefghi 

Through experimentation, I found that I can avoid behavior with a backtick:

 PS C:\> echo `-abc.def.ghi -abc.def.ghi 

But why is this happening? What is the main part of PowerShell syntax that I don’t understand?

+6
source share
2 answers

I parsed Microsoft.PowerShell.Utility to look at the Write-Output code, nothing special, it just iterates through an InputObject and passes each WriteObject method implemented by the current ICommandRuntime .

Disassembled <code> Write-Output </code>

I assume that the tokenizer processing the text is trying to match anything, starting with - with the declared parameter. Otherwise, it passes it along the pipeline as an element in -InputObject . Since . cannot be part of the variable name and therefore cannot be part of the switch name, it can separate it before performing an if check, and when it turns out to be a parameter, it does not join it. with the rest of the token. Thus, you may find a minor error.

When using a back tick or quotation mark, this does not make this mistake, as it can symbolize everything.

These are all speculations, although I would really like you to receive an authoritative answer, just like you.

Edit

Evidence of what I say:

 PS> echo -NoEnumerate.foo .foo 
+5
source

FYI is not sure if George tried to mention this or not, but echo is an alias for Write-Output in PowerShell.

 PS C:\temp> Get-Alias echo CommandType Name ModuleName ----------- ---- ---------- Alias echo -> Write-Output 

In the echo -abc.def.ghi example, the analyzer sees an undefined hyphen as a prefix for the parameter / switch name. The period is also of particular importance. Parameters cannot contain periods, so the parser considers this as a line terminator.

In general, Write-Output sees it as an array, so it is positioned positionally for -InputObject

(I hope I'm not completely mistaken in my statements.)

+1
source

Source: https://habr.com/ru/post/982961/


All Articles