The MD5 hash value is different from Bash and PHP [duplicate]
By default, echo
includes a newline character at the end of the output. However, PHP and the online sites you used do not include the newline. To suppress the newline character, use the -n
flag:
echo -n "hello" | md5sum
Output:
5d41402abc4b2a76b9719d911017c592 -
See: help echo
or with printf:
printf "%s" "hello" | md5sum
@Cyrus's answer is exactly on point with how to resolve this - to explain, when using echo
it will output a newline at the end of the string. As you can see on this online output, hello
with a newline outputs exactly the MD5 you were getting previously. Using -n
suppresses the newline, and will then give you the result you expected.
Edit:
You can see it clearly if you output it to hexdump
, which shows the hexadecimal of the bytes there.
$ echo "str_example" | hd
00000000 73 74 72 5f 65 78 61 6d 70 6c 65 0a |str_example.|
See the 0a
(\n
) in the end of the string
$ echo -n "str_example" | hd
00000000 73 74 72 5f 65 78 61 6d 70 6c 65 |str_example|
With -n
echo doesn't put a new line (\n
) in the end
Now with a empty string
$ echo "" | hd
00000000 0a |.|
Just the New Line character
$ echo -n "" | hd
Empty string, so hexdump
shows no output