Title: Call native operator · Issue #13068 · PowerShell/PowerShell · GitHub
Open Graph Title: Call native operator · Issue #13068 · PowerShell/PowerShell
X Title: Call native operator · Issue #13068 · PowerShell/PowerShell
Description: Problem Statement Currently, there are cases where cutting and pasting a native command line fails to run as expected in PowerShell. This may be due to incorrect parsing of quotes meant to be passed to the native command or use of PowerS...
Open Graph Description: Problem Statement Currently, there are cases where cutting and pasting a native command line fails to run as expected in PowerShell. This may be due to incorrect parsing of quotes meant to be passe...
X Description: Problem Statement Currently, there are cases where cutting and pasting a native command line fails to run as expected in PowerShell. This may be due to incorrect parsing of quotes meant to be passe...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/13068
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Call native operator","articleBody":"## Problem Statement\r\n\r\nCurrently, there are cases where cutting and pasting a native command line fails to run as expected in PowerShell. This may be due to incorrect parsing of quotes meant to be passed to the native command or use of PowerShell syntax that is not meant to be interpreted as PowerShell. PowerShell has a `--%` special argument when used with native commands treats the rest of the arguments as literals passed to the native command, but has several issues:\r\n\r\n1. It is not discoverable, users need to know about this special parameter ahead of time\r\n2. `|`, `\u0026\u0026`, and `||` take precedence, so: `wsl --% ls | less` would execute `wsl ls` and pipe the results to `less` running in PowerShell rather than `less` running in wsl\r\n3. If you cut and paste a command line, you would need to edit the line to insert `--%` towards the beginning\r\n4. On Unix systems, the args after --% are passed verbatim w/o globbing where native commands on Unix expect the shell to perform globbing\r\n\r\n## Proposed technical implementation details\r\n\r\nProposal is to introduce a new `--%` (Call Native) operator.\r\n\r\nAny text content after this operator will call into the \"default shell\" of the OS to execute. On Windows, this would be cmd.exe and on Unix-based systems this would be /bin/sh. This resolves the globbing issue on Unix-based systems, and also allow `%variable%` expansion on Windows. Unlike `--%` switch, this also means that `|`, `\u0026\u0026`, and `||` are treated as part of the native command line.\r\n\r\nThis means that these two are functionally the same:\r\n\r\n```powershell\r\nwsl --% ls $foo `\u0026`\u0026 echo $PWD\r\n```\r\n\r\n```powershell\r\n--% wsl ls $foo \u0026\u0026 echo $PWD\r\n```\r\n\r\nwhere `$foo` and `$PWD` is evaluated by the shell within WSL. Note that in the first example, you would have to know to escape `\u0026\u0026` to have it execute within WSL instead of within PowerShell.\r\n\r\nTo pipe output from such execution back into PowerShell, the user is required to store the results into a variable first:\r\n\r\n```powershell\r\n$out = --% ls *.txt\r\n$out | select-string hello\r\n```\r\n\r\nNote that unlike the current `\u0026` call operator, you cannot use any PowerShell syntax, so:\r\n\r\n```powershell\r\n--% $commandline\r\n```\r\n\r\nwould not resolve `$commandline` as a variable first by PowerShell, but instead pass `$commandline` to the default shell to process unresolved.\r\n\r\nThe cut \u0026 paste problem is solved by simply pasting after `--% ` is typed.\r\n\r\nThe above example for wsl would look like:\r\n\r\n```powershell\r\n--% wsl ls | less\r\n```\r\n\r\nwhere the intent is to have that whole line execute within the WSL Linux instance.\r\n\r\n## Discoverability\r\n\r\nUsers already familiar with `--%` as a switch may easily transition to using this new operator where it makes sense. For new users, `--%` is unique so that search engines find it easily related to PowerShell.\r\n\r\n## Alternate Considerations\r\n\r\n`\u0026!` and `\u0026n` were proposed as the sigil, but there was push back because `\u0026!` is a valid operator in some languages making a web search for documentation more difficult. There was also a concern whether visual similar to `\u0026` call operator would be confusing to users.\r\n\r\nThere is question about supporting line-continuation when using this new operator. I would suggest that we do not support it initially.\r\n\r\nA cmdlet solution instead of an operator solution was proposed. We believe this doesn't solve the \"cut \u0026 paste\" problem as now you need to know to put the pipeline in single quotes and/or escape special characters. We do believe a cmdlet as in `Invoke-NativeCommand` (noun to be determined) would be useful as an additional option instead of replacing the need for an operator.\r\n\r\n## Related issues\r\n\r\nThis should also solve these issues:\r\n\r\nhttps://github.com/PowerShell/PowerShell/issues/12491\r\nhttps://github.com/PowerShell/PowerSHell/issues/1761\r\n","author":{"url":"https://github.com/SteveL-MSFT","@type":"Person","name":"SteveL-MSFT"},"datePublished":"2020-06-30T20:53:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":210},"url":"https://github.com/13068/PowerShell/issues/13068"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1118aab1-5d2e-85f6-83dd-479f0ab01c7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A62:24328B:A334E63:D866593:696E2021 |
| html-safe-nonce | aa3c2d72c665b0373166fd08ace1dbf384d91dee28d0ff4b913c568430c6d45f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTYyOjI0MzI4QjpBMzM0RTYzOkQ4NjY1OTM6Njk2RTIwMjEiLCJ2aXNpdG9yX2lkIjoiNDI3MDQ0MzAwODczODg2MTA4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 57a4d83fd923b9908317ea7d9501233c2016cb3d8c596414a36db20003e79b98 |
| hovercard-subject-tag | issue:648485701 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/13068/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e6a6f95d9be1191cbf63cef43de2092b9866e561c850ae6f27e387719d7223b/PowerShell/PowerShell/issues/13068 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e6a6f95d9be1191cbf63cef43de2092b9866e561c850ae6f27e387719d7223b/PowerShell/PowerShell/issues/13068 |
| og:image:alt | Problem Statement Currently, there are cases where cutting and pasting a native command line fails to run as expected in PowerShell. This may be due to incorrect parsing of quotes meant to be passe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SteveL-MSFT |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2fbe8cba5e260284c10af515699ff9bb2d6ace05ab6c2e2e585b71d93b2812c3 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7fb3bc5c995a876085291706b75cf9b08900c338 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width