Title: Parsing of compound command-line tokens into arguments is surprising · Issue #6467 · PowerShell/PowerShell · GitHub
Open Graph Title: Parsing of compound command-line tokens into arguments is surprising · Issue #6467 · PowerShell/PowerShell
X Title: Parsing of compound command-line tokens into arguments is surprising · Issue #6467 · PowerShell/PowerShell
Description: The primary purpose of this issue to highlight how the existing behavior may be surprising to newcomers (and even for seasoned users hard to remember and therefore predict in specific situations). Given that the behavior has been around ...
Open Graph Description: The primary purpose of this issue to highlight how the existing behavior may be surprising to newcomers (and even for seasoned users hard to remember and therefore predict in specific situations). ...
X Description: The primary purpose of this issue to highlight how the existing behavior may be surprising to newcomers (and even for seasoned users hard to remember and therefore predict in specific situations). ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6467
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parsing of compound command-line tokens into arguments is surprising","articleBody":"* The primary purpose of this issue to highlight how the existing behavior may be surprising to newcomers (and even for seasoned users hard to remember and therefore predict in specific situations).\r\n\r\n* Given that the behavior has been around since the beginning, changing it is probably not an option, but _documenting_ how the behavior may be surprising is worth doing; while _some_ aspects of the behavior can be inferred from [`about_Parsing`](https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Parsing.md), the overall implications aren't clear.\r\n * @SteveL-MSFT: This issue subsumes #3217, which therefore no longer needs the `Documentation Needed` label.\r\n\r\n* While this may ultimately become a [documentation-repo issue](https://github.com/PowerShell/PowerShell-Docs/issues), I would first like to solicit some feedback as to what code changes, if any, should be considered, after all (there are edge cases), and as to whether my summary is accurate.\r\n\r\n* While there may be good _technical_ reasons for these behaviors, the problem is that they may be unexpected, especially given the deviation from how `cmd.exe` and `bash` handle these scenarios.\r\n\r\n* All surprising behaviors can be avoided with proper _quoting_ - which is also worth pointing out in the docs - but the need to quote negates one of the convenient aspects of argument syntax. If at the end of the day you cannot remember whether a particular token _requires_ quoting, your choices are to resort to trial and error, or to quote _everything_, which is cumbersome.\r\n\r\nNote: By **_compound command-line token_** I mean a **logically single token composed of the _following syntax constructs_ by _direct concatenation_**:\r\n \r\n* barewords (unquoted strings without special meaning; e.g., `foo`)\r\n* quoted strings (e.g.; `'bar'`, `\"baz\"`)\r\n* variable references (`$foo`) and subexpressions (`$(...)`)\r\n\r\nExamples:\r\n\r\n* `bar${foo}` and `${foo}bar` and `$($foo)bar`\r\n* `foo\"bar\"` and `\"bar\"foo`\r\n\r\n**The surprising aspects are**:\r\n\r\n### Asymmetry: Whether a quoted string or subexpression comes _first_ matters:\r\n\r\nPlacing the quoted string or subexpression _first_ results in *2* arguments:\r\n\r\n```powershell\r\nPS> Write-Output 'hi'there\r\nhi\r\nthere\r\n\r\n> Write-Output $($env:HOME)/Documents\r\n/Users/jdoe\r\n/Documents\r\n```\r\n\r\nOtherwise, a _single_ argument is passed, as expected:\r\n\r\n```powershell\r\nPS> Write-Output hi'there'\r\nhithere\r\n\r\nPS> Write-Output Documents:$($env:HOME) # Note: just $env:HOME without $() is not affected.\r\nDocuments:/Users/jdoe\r\n```\r\n\r\n### `.` following a leading quoted string is interpreted as [string] property access\r\n\r\n```powershell\r\nPS> Write-Output 'hi'.there\r\n # NO output, because an attempt is made to access property .there on the string instance\r\n```\r\n\r\n### `.` as the initial char. followed by a quoted string results in *2* arguments\r\n\r\n```powershell\r\nPS> Write-Output .'hi'\r\n.\r\nhi\r\n```\r\n\r\n### `-` as the initial char. in something that turns out _not_ to be a parameter name results in *2* arguments with the presence of `:` or `.` \r\n\r\nSee also: #6292 and #6360.\r\n\r\n```powershell\r\nPS> Write-Output -foo:bar\r\n-foo:\r\nbar\r\n\r\nPS> Write-Output -ip=10.0.0.1\r\n-ip=10\r\n.0.0.1\r\n```\r\n### `-` as the initial char. causes embedded variable references not to be expanded\r\n\r\nSee also: #4624.\r\n\r\n```powershell\r\nPS> Write-Output a$HOME\r\na/home/jdoe # e.g.; $HOME was expanded, as expected\r\n\r\nPS> Write-Output -a$HOME\r\n-a$HOME # !! $HOME was NOT expanded\r\n```\r\n\r\nNote that the problem only surfaces if the there's at least 1 literal char. following the `-` before the variable reference; for instance, in `-$HOME` the variable _is_ expanded.\r\n\r\n\r\nEnvironment data\r\n----------------\r\n\r\nWritten as of:\r\n\r\n```powershell\r\nPowerShell Core v6.0.2\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2018-03-22T20:14:21Z","url":"https://github.com/PowerShell/PowerShell/issues/6467"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:689b4ca0-d24e-4e1b-4009-56b662c7313d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA74:A76E8:134B6C:1A5AA1:6AFD4C9C |
| html-safe-nonce | 57c9d853d489560a41d750d775beb57002ad169a718a63b5cf00483773c99c6f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTc0OkE3NkU4OjEzNEI2QzoxQTVBQTE6NkFGRDRDOUMiLCJ2aXNpdG9yX2lkIjoiNzg3NTAyMTMyNjI1MjMxMDA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9003db3bfca8b67b12e9e8e023b8f84341130b6f41f103060178c4ed82497896 |
| hovercard-subject-tag | issue:307798025 |
| 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/6467/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4f7814b59a05899f37aefb48feed2214e651441c0e8fd638b5cb01662444ace8/PowerShell/PowerShell/issues/6467 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4f7814b59a05899f37aefb48feed2214e651441c0e8fd638b5cb01662444ace8/PowerShell/PowerShell/issues/6467 |
| og:image:alt | The primary purpose of this issue to highlight how the existing behavior may be surprising to newcomers (and even for seasoned users hard to remember and therefore predict in specific situations). ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| 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 | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width