Piping Text To An External Program Appends A Trailing Newline

后端 未结 2 1395
天命终不由人
天命终不由人 2021-01-05 04:04

I have been comparing hash values between multiple systems and was surprised to find that PowerShells hash values are different than that of other terminals.

Linux t

2条回答
  •  独厮守ぢ
    2021-01-05 04:41

    Linux terminals and PowerShell use different encodings. So real bytes produced by echo -n "string" are different. I tried it on my Linux Mint terminal and Windows 10 PowerShell. Here what I got:

    Linux Mint:

    73 74 72 69 6E 67
    

    Windows 10:

    FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00
    

    It seems that Linux terminals use UTF-8 and Windows PowerShell uses UTF-16 with a BOM. Also in PowerShell you cannot use '-n' parameter for echo. So echo places newline characters \r\n (0D 00 0A 00) at the end of the "string".

    Edit: As mklement0 said below, Windows PowerShell uses ASCII by default when piping.

提交回复
热议问题