Open philcerf opened 3 weeks ago
btw: The background to this is that I wanted to be able to start scripts from a shell script like in:
$ pwsh -Command - <'END'
some powershell script
goes here
END
Oh and I should mention why I titled "(some) commands":
E.g. just
$ pwsh -Command - <<'END'
Get-Process
END
seems to work fine, even without the extr newline before the END
, that makes the above script working.
Just noted, that it doesn't need to be an extra newline to make it work. Seems any even non-newline-terminated character after the newline from the last actual command line makes it work.
For example, the following doesn't work:
cmd="$( cat <<'END'
Get-Process |
Where-Object {$_.Name -ILike '*explorer*'}
END
)"
printf '%s\n' "$cmd" | powershell -Command -
while when there's an extra space (or e.g. \n
) after the \n
it works:
cmd="$( cat <<'END'
Get-Process |
Where-Object {$_.Name -ILike '*explorer*'}
END
)"
printf '%s\n ' "$cmd" | powershell -Command -
Interestingly, it seems if the extra char is missing, then powershell doesn't even execute the command. Originally I had a quoting error in my code (the \
before the $
).
In:
cmd="$( cat <<'END'
Get-Process |
Where-Object {\$_.Name -ILike '*explorer*'}
END
)"
printf '%s\n' "$cmd" | powershell -Command -
No error messages are printed, while in:
cmd="$( cat <<'END'
Get-Process |
Where-Object {\$_.Name -ILike '*explorer*'}
END
)"
printf '%s\n ' "$cmd" | powershell -Command -
there are.
As noted in #3223, it is multiline statements that need an extra trailing newline in order to be recognized as a complete statement by the REPL-like behavior exhibited by -Command -
, but the linked issue was aimed at a more fundamental fix: doing away with the REPL behavior, which turned out not to be an option due to backward compatibility.
So, thank your opening this a separate issue, which can be dealt with in the context of the by-design REPL behavior. (I don't have an explanation for why a space in lieu of an extra newline seemingly also works from Bash.)
The extra trailing newline required to terminate a multiline statement appears to be by design (the same behavior applies in an interactive session with PSReadLine
unloaded).
The real bug here is that an incomplete statement is ignored; quoting @SeeminglyScience from https://github.com/PowerShell/PowerShell/issues/15331#issuecomment-830816330:
Looks like the logic for handling
IncompleteInput
parse errors isn't there. That should probably be its own issue (if it isn't already). Also probably should print parse errors for that matter.
Prerequisites
Steps to reproduce
I see the following in current PowerShell versions back to the days of
powershell.exe
.Having the following files:
(i.e. happens with both, LF and CRLF newlines)
and executing them via
-File
works as expected:But If I do the same from stdin:
there's no output at all and the exit code is still
0
.It does suddenly work, if I add another trailing newline (either LF or CRLF) at the end of the file.
What also works is:
This is executed from
bash
, and the string passed as argv to-Command
contains a LF newline, i.e. binary it looks like:So if the command is given as string, it works even without terminating newline for the last line.
Any ideas why it doesn't work when reading the command from stdin?
btw: I do the this on Windows and invoking Cygwin.
Thanks, Philippe
Expected behavior
Actual behavior
Error details
No response
Environment data
gives no output either ;-)