Only get hash value using md5sum (without filename)

John Doe picture John Doe · Sep 9, 2010 · Viewed 128.2k times · Source

I use md5sum to generate a hash value for a file. But i only need to receive the hash value, not the file name.

md5=`md5sum ${my_iso_file}`
echo ${md5}

3abb17b66815bc7946cefe727737d295 ./iso/somefile.iso

How can i 'strip' the file name and only remain the value ?

Answer

Peter.O picture Peter.O · Apr 25, 2011

A simple array assignment works... Note that the first element of a bash array can be addressed by just the name without the [0] index, ie, $md5 contains only the 32 chars of the md5sum.

md5=($(md5sum file))
echo $md5
# 53c8fdfcbb60cf8e1a1ee90601cc8fe2