Skip to content

PowerShell should support passing verbatim args #12975

@bitcrazed

Description

@bitcrazed

Following a conversation with @SteveL-MSFT where he confirmed I am not (entirely) insane 😜

PowerShell currently makes if VERY difficult to

  1. Specify that an arbitrary set of command-line args gets passed to an exe, verbatim
  2. That the same command-line also supports PowerShell command chaining, etc.

For example, trying to pass a Linux command-line cd / && ls . | cowsay to wsl in PowerShell 7+:

wsl cd / && ls .

Expected: Directory listing of the root of the users' default Linux distro
Actual: Error reporting that cowsay is not a recognized command:
image

Note: to perform the above command, you'll need to enable WSL and a Linux distro. Within your default distro, you'll also need to install cowsay via your distro's package manager. For Ubuntu and Debian, you can do this via sudo apt install cowsay

As can be seen from the syntax color highlighting, PowerShell is parsing && as a PowerShell command continuation, not as a part of the command to be passed to wsl.

So, how about wrapping the args in quotes?
image

Doesn't work because that would make arg[1] 'cd / && ls . | cowsay' which wouldn't make sense to bash.

@SteveL-MSFT recommended prefixing the command-line args with --% to indicate that PowerShell should pass the remaining command-line verbatim. Alas, no joy here - notice the syntax highlighting which indicates that the parser thinks the && is a PowerShell command which, in this case, it is not!
image

Boo! 😟

Okay, so how about escaping the && with a ````` (backtick)?
image

Oh! PowerShell thinks that I meant to spawn the remaining command as a job. NOPE!

How about escaping each &?
image

Closer!

Now, how about also escaping the pipe operator:
image

HUZZAH! 🙌

Escaping many symbols in lengthy command-lines could get tedious pretty quickly, especially when pasting more lengthy & complex bash command-lines into WSL, or complex page splits into wt (Windows Terminal):
image

While --% may be useful (when fixed) in some cases to pass the remainder of this command-line to the tool being executed, commands couldn't be chained.

So, ideally, PowerShell should (also) provide a way of escaping a specific portion of a command-line to the preceding tool, but then continue treating the remaining command-line as PowerShell script.

Perhaps enclosing the command-line segment in some of the following delimiters would work?

  • Pointy-braces: < ... >, e.g. wsl <cd / && ls . | cowsay>
  • Square-braces: [ ... ], e.g. wsl [cd / && ls . | cowsay]
  • Curly-braces: { ... }, e.g. wsl {cd / && ls . | cowsay}
  • Double-backticks
  • Arrow: <-- ... --<<< , e.g. wsl <-- cd / && ls . | cowsay --<<< 😜

... but will leave the choice of the enclosing delimiters to the language designers 😁

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions