Title: CLI: Stdin input does not implicitly become pipeline input · Issue #9497 · PowerShell/PowerShell · GitHub
Open Graph Title: CLI: Stdin input does not implicitly become pipeline input · Issue #9497 · PowerShell/PowerShell
X Title: CLI: Stdin input does not implicitly become pipeline input · Issue #9497 · PowerShell/PowerShell
Description: Currently, stdin input streamed to pwsh from the outside: does not automatically become pipeline input; accessing stdin input currently requires constructing a separate pipeline in PowerShell code using $Input only automatically becomes ...
Open Graph Description: Currently, stdin input streamed to pwsh from the outside: does not automatically become pipeline input; accessing stdin input currently requires constructing a separate pipeline in PowerShell code ...
X Description: Currently, stdin input streamed to pwsh from the outside: does not automatically become pipeline input; accessing stdin input currently requires constructing a separate pipeline in PowerShell code ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/9497
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"CLI: Stdin input does not implicitly become pipeline input","articleBody":"Currently, _stdin_ input streamed to `pwsh` _from the outside_:\r\n\r\n* does _not_ automatically become pipeline input;\r\n * accessing stdin input currently requires constructing a separate pipeline in PowerShell code using `$Input`\r\n\r\n* only automatically becomes input to _interactive prompts_, such as `Read-Host` calls or the automatic prompts displayed when mandatory arguments are missing.\r\n\r\nThis has the following implications:\r\n\r\n**You cannot directly pipe data to PowerShell commands from the outside:**\r\n\r\nContrast the following two commands:\r\n\r\n```sh\r\n# Bash:\r\n# stdin input is \"relayed\" to the 'wc' utility\r\n$ printf 'one\\ntwo' | bash -c 'wc'\r\n2\r\n```\r\n\r\n```powershell\r\n# PowerShell:\r\nPS> printf 'one\\ntwo' | pwsh -noprofile -c 'Measure-Object -Word | % Words'\r\n0 # !! Measure-Object did NOT receive stdin input.\r\n\r\n# Explicit reference to $Input in an internal pipeline is required:\r\nPS> printf 'one\\ntwo\\n' | pwsh -noprofile -c '$Input | Measure-Object -Word | % Words'\r\n2\r\n```\r\nprintf 'one\\ntwo\\n' | pwsh -noprofile -c 'Measure-Object -Word'\r\n\r\n**If you're calling an _advanced script that binds pipeline input_, it cannot be invoked with just `-File`, because it won't receive pipeline input**:\r\n\r\n```powershell\r\n# Create an advanced script that echoes each pipeline input enclosed in [...]\r\n'[CmdletBinding()] param([Parameter(ValueFromPipeline)] $var) process { \"[$var]\" }' > t.ps1\r\n\r\n# Call from either Bash or PowerShell (same behavior).\r\nPS> echo hi | pwsh -noprofile -file t.ps1\r\n[] # !! t.ps1 did NOT receive stdin input\r\n\r\n# The workaround is again non-obvious and cumbersome:\r\nPS> echo hi | pwsh -noprofile -command '$Input | ./t.ps1'\r\n[hi] # OK\r\n```\r\n\r\n**Similarly, if you author a pipeline-binding advanced script as _shebang-line-based script on Unix_, it cannot receive stdin input directly**:\r\n\r\n```powershell\r\n# Create a shebang-line-based script...\r\n@'\r\n#!/usr/bin/env pwsh\r\n[CmdletBinding()] param([Parameter(ValueFromPipeline)] $var) process { \"[$var]\" }\r\n'@ > foo\r\n\r\n# ... and make it executable.\r\nchmod +x foo\r\n\r\n# Call from either Bash or PowerShell (same behavior).\r\nPS> 1..2 | ./foo\r\n[] # !! Pipeline input wasn't bound, not even with pipeline input from PowerShell.\r\n``` \r\n\r\n---\r\n\r\nPowerShell's behavior here is at odds with traditional shells and arguably makes the typical use case unnecessarily difficult.\r\n\r\nNote: **I'm unclear on whether this can be fixed without breaking existing behavior.**\r\n\r\nThe challenge is to know when to provide stdin input via an _implied PowerShell pipeline_, and when to use stdin input line by line, on demand, to respond to _interactive prompts_.\r\n\r\nE.g., the following currently works - the two distinct `Read-Host` prompts are \"answered\" by 1 stdin input line each:\r\n\r\n```powershell\r\n$ 'one\\ntwo\\n' | pwsh -noprofile -c 'read-host first; get-date; read-host second'\r\nfirst: one\r\none\r\n\r\nMonday, April 29, 2019 10:52:04 AM\r\nsecond: two\r\ntwo\r\n```\r\n\r\n# Environment data\r\n\r\n```none\r\nPowerShell Core 6.2.0\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2019-04-29T15:05:19Z","url":"https://github.com/PowerShell/PowerShell/issues/9497"}
| 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:574a28fa-f10d-9fd2-71e4-55c54cf8b19e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C462:160FD0:26473C0:35511D9:6AFD4303 |
| html-safe-nonce | 7b7c17780640d1d283eae49e39df087a0829422fd4911ab242d6495151747530 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDYyOjE2MEZEMDoyNjQ3M0MwOjM1NTExRDk6NkFGRDQzMDMiLCJ2aXNpdG9yX2lkIjoiMTI0MTk3NTAxMTg3OTUwMjU5NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 26a609d57ac2ed4f1048fe438de06a75908c3156db8b380a5edd25f0b12ae92f |
| hovercard-subject-tag | issue:438363576 |
| 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/9497/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0cf304241c03a759be1c66d7ca4083dd3469b90ee1fe5fd2f377643b7e668151/PowerShell/PowerShell/issues/9497 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0cf304241c03a759be1c66d7ca4083dd3469b90ee1fe5fd2f377643b7e668151/PowerShell/PowerShell/issues/9497 |
| og:image:alt | Currently, stdin input streamed to pwsh from the outside: does not automatically become pipeline input; accessing stdin input currently requires constructing a separate pipeline in PowerShell code ... |
| 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 | bdbacc25b921224ff0befcb9e629560a1255be51aec9991d6e3f95b5e9151474 |
| 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 | 556dd37bb0aceffb25724e151b5d858b3265b40b |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width