Difference between PowerShell's echo and CMD's echo

319 views Asked by At

I get the following in PowerShell:

D:\> echo "Apple Pie" | git hash-object --stdin
157cb7be4778a9cfad23b6fb514e364522167053

D:\> "Apple Pie" | git hash-object --stdin
157cb7be4778a9cfad23b6fb514e364522167053

but in CMD.exe:

C:\>echo "Apple Pie" | git hash-object --stdin
bb3918d5053fea31fc9a58fae1e5bdeabe3ec647

In a PluralSight video, I see a different value from what seems to be a Mac console:

enter image description here

What is the exact value piped from echo in each case?

I get a different hash if I go to one of those online SHA1 generators and enter the string Apple Pie. From those I get:

8d69b7365f59237d3fb052c1f2f15ea33457fe51

1

There are 1 answers

2
JPBlanc On BEST ANSWER

As far as I understand :

Using CMD :

echo Apple Pie|git hash-object --stdin

return the same think as the following in PowerShell

"Apple Pie" | git hash-object --stdin

That is to say :

157cb7be4778a9cfad23b6fb514e364522167053

@Mofi seems to be right, you can reproduce the CMD result in Powershell using :

'"Apple Pie" ' | git hash-object --stdin

To explain the Mac OS one : To obtain 157cb7be4778a9cfad23b6fb514e364522167053 the real list of chars that is hashed is 'Apple Pie\r\n' (with carage return line feed), in Mac or linux like command line it's 'Apple Pie\r'.

If you want to test this : put 'Apple Pie' in a text file with a cariage return and save it as a Windows text style (CR+LF), and use git hash-object yourfile.txt. Then save it in Linux style (LF) and test again, you will find your two hashes.


The part about \r\n.

"Apple Pie" | where {$_.length -eq 9} shows that the string is exactly 9 characters long

For me it's because in your case the pipe is between two PowerShell parts, the pipe transmit an object. When the pipe is between PowerShell and an external EXE then the \r\n are added. Here is a way to test that with a small exe file written in C# :

using System;
namespace C_Param
{
  class Program
  {
    static void Main(string[] args)
    {
      string line = Console.In.ReadToEnd();
      foreach (char character in line){
        Console.WriteLine(String.Format("{0:X2}", Convert.ToByte(character)));
      }
    }
  }
}

The result in a PowerShell console is :

"Apple Pie" | .\C_Param.exe
41
70
70
6C
65
20
50
69
65
0D
0A

The result in a CMD console is :

echo "Apple Pie" | .\C_Param.exe
22
41
70
70
6C
65
20
50
69
65
22
20
0D
0A

QED ?