In powershell(v2) condition,I run the command,it goes gibberish.
echo "すみません" | ruby -e "puts gets"
?????
My powershell is V2.How should I get the correct output?
CodePudding user response:
As implied by the answer to your related question, you need to set $OutputEncoding
/ [Console]::OutputEncoding
first to match the character encoding that ruby
expects / outputs; e.g., for UTF-8:
# Make PowerShell both send and receive data as UTF-8 when talking to
# external (native) programs.
# Note:
# * In *PowerShell (Core) 7 *, $OutputEncoding *defaults* to UTF-8.
# * You may want to save and restore the original settings.
$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::new()
echo "すみません" | ruby -e "puts gets"
As for what you tried:
With Windows PowerShell using ASCII(!) encoding by default when sending data via the pipeline to external programs (see below), characters such as す
can NOT be represented, and get "lossily" translated to literal ?
characters.
Since none of the characters in your input string have an ASCII representation, the (ASCII representation of) verbatim string ?????
was sent to ruby
.
Background information:
In PowerShell it is the $OutputEncoding
preference variable that determines the encoding used for sending data TO external programs, and in Windows PowerShell it defaults to ASCII(!), i.e. only characters in the 7-bit ASCII subrange of Unicode can be represented.
In PowerShell (Core) 7 , $OutputEncoding
now more sensibly defaults to (BOM-less) UTF-8.
Unfortunately, with respect to receiving data from external programs, where [Console]::OutputEncoding
matters for correct interpretation of the output, even PowerShell (Core) 7 still uses the legacy OEM code page by default as of v7.3.1, which means that UTF-8 output from external programs is by default misinterpreted - see GitHub issue #7233 for a discussion.