Creating an MD5 hash of a string in bash

Make sure that you are not including the new line character (\n) as well in your string, there are cases where it might be included without you explicitly writing it.
For example if you use the output of an echo command in bash, it will automatically add a new line character to the end and will cause your hash to be not the one you would like to have.
For example, the MD5 hash of ‘’ (without the quotes) should be 16c00d9cfaef1688d4f2ddfb11b60f46 but if you execute the following you will see you will get a different result.
echo '' | md5sum
01c46835dcb79be359e0b464ae6c6156 -

To avoid this error, use the -n option for echo that will direct the command not to output a trailing new line character.
echo -n '' | md5sum
16c00d9cfaef1688d4f2ddfb11b60f46 -

This post is also available in: Greek

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.