There are many examples to do this, but some of them are not equivalent because some of them explicitly or implicitly include the newline, and some others do not.
I would like to clearly specify which of the popular methods includes the newline and which are not.
Here are some examples along to calculating the md5 hash WITHOUT trailing newline (CORRECT):
Using a file with text:
$ echo -n "test" > test.txt
$ wc test.txt
0 1 4 test.txt
$ md5sum test.txt
098f6bcd4621d373cade4e832627b4f6 test.txt
Note: -n
in echo
means: "do not output the trailing newline".
Using echo
with -n
inline:
$ echo -n "test" | md5sum
098f6bcd4621d373cade4e832627b4f6 -
Using printf
:
$ printf "%s" "test" | md5sum
098f6bcd4621d373cade4e832627b4f6 -
Using only md5sum
command:
(Let's write md5sum
, press Enter then write string test
and then press double combination Ctrl+d)
$ md5sum
test098f6bcd4621d373cade4e832627b4f6 -
Using md5sum -
command:
(Let's write md5sum -
, press Enter then write string test
and then press double combination Ctrl+d)
$ md5sum -
test098f6bcd4621d373cade4e832627b4f6 -
Here are some examples along to calculating the md5 hash WITH trailing newline (SO NOT CORRECT):
Using a file with text:
$ echo "test" > test_n.txt
$ wc test_n.txt
1 1 5 test_n.txt
$ md5sum test_n.txt
d8e8fca2dc0f896fd7cb4cb0031ba249 test_n.txt
Using echo
WITHOUT -n
inline:
echo "test" | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
Using here strings:
$ md5sum <<< "test"
d8e8fca2dc0f896fd7cb4cb0031ba249 -
Using only md5sum
command but with Enter key after writing the text:
(Let's write md5sum
, press Enter then write string test
and then press agaien Enter and once combination Ctrl+d)
$ md5sum
test
d8e8fca2dc0f896fd7cb4cb0031ba249 -
Using md5sum -
command but with Enter key after writing the text:
(Let's write md5sum -
, press Enter then write string test
and then press agaien Enter and once combination Ctrl+d)
$ md5sum -
test
d8e8fca2dc0f896fd7cb4cb0031ba249 -
md5 -s abcdefg