Bash – How to pipe md5 hash result in shell

bashhashsumshell-scriptUbuntu

I am looking for a simple way to pipe the result of md5sum into another command. Something like this:

$echo -n 'test' | md5sum | ...

My problem is that md5sum outputs not only the hash of the string, but also an hypen, which indicates that the input came from stdin. I checked the man file and I didn't find any flags to control output.

Best Answer

You can use the command cut; it allows you to cut a certain character/byte range from every input line. Since the MD5 hash has fixed length (32 characters), you can use the option -c 1-32 to keep only the first 32 characters from the input line:

echo -n test | md5sum | cut -c 1-32

Alternatively, you can tell cut to split the line at the every space and output only the first field: (note the quotes around the space character)

echo -n test | md5sum | cut -d " " -f 1

See the cut manpage for more options.

Related Question