I am executing the below command in bash
filehash=`openssl dgst -sha1 $filename`
When I echo $filehash
I get this
SHA1(myfile.csv)=
bac9c755bac9709fa8966831d1731dff07e28e6c
How do I only get the hash value stored and not the rest of the string i.e.
bac9c755bac9709fa8966831d1731dff07e28e6c
Thanks
Through sed
,
filehash=`openssl dgst -sha1 $filename | sed 's/^.*= //'`
It removes all the characters upto the =
(equal symbol followed by a space).
In a lot of ways with pure Bash, e. g. by truncating string from start up to last space:
filehash="$(openssl dgst -sha1 "$filename")"
filehash="${filehash##* }"
or using possibility to obtain reverse (-r
) notation from openssl
:
read filehash __ < <(openssl dgst -sha1 -r "$filename")
Obviously awk
, sed
or any other external utility is overkill here. And please note quotes.
Either with sed or awk (if you have them installed):
hash=$(echo $filehash | awk -F '=' '{ print $2 }')
for example.