Git powershell调用&;操作员:设置输出编码

Git powershell调用&;操作员:设置输出编码,git,powershell,encoding,Git,Powershell,Encoding,直接执行时,一切都按预期进行: & git status # OK: "modified: awél.txt" 存储在变量中 $aargh = & git status $aargh # NOT OK: "modified: aw├®l.txt" 如何告诉它使用什么编码?如果将外部程序的输出打印到屏幕上,通常不会出现字符编码问题,但如果捕获它或通过管道将其发送到另一个命令,则可能会出现问题,因为PowerShell会在该过程中将输出解码为.NET字符串 为了让Po

直接执行时,一切都按预期进行:

& git status

# OK: "modified:   awél.txt"
存储在变量中

$aargh = & git status
$aargh

# NOT OK: "modified:   aw├®l.txt"

如何告诉它使用什么编码?

如果将外部程序的输出打印到屏幕上,通常不会出现字符编码问题,但如果捕获它或通过管道将其发送到另一个命令,则可能会出现问题,因为PowerShell会在该过程中将输出解码为.NET字符串

为了让PowerShell正确识别git的输出,您必须:

  • 了解git在输出中使用的字符编码
    git

  • 并将
    [控制台]::outputeneCoding
    设置为该编码(在Windows上,默认为系统的旧OEM代码页)

git
使用UTF-8,因此在捕获
git
的输出之前必须运行以下命令:

[Console]::OutputEncoding = [System.Text.Encoding]::Utf8
请注意,是否[需要]使用
&
调用外部程序与字符编码问题无关