linuxbashawkmd5sumsha1sum

Linux bash: Compare hash strings without setting variables


I want a simple bash command to compare two hash values that outputs whether they are the same. Here's what I've tried:

md5sum file1 | awk '{print $1}' # outputs hash value without filename

md5sum file1 > md5sum file2 # no output even though files/hashes differ

I've tried variations on the following with no success so far:

[ md5sum states.txt | awk '{print $1}' == md5sum states_copy.txt | awk '{print $1}' ]`

[ (md5sum states.txt | awk '{print $1}') == (md5sum states_copy.txt | awk '{print $1}') ]

I'm open to a script or multi-line bash solution, or using shasum, but I'm new to Linux and bash so trying to keep it as simple as possible.

I'm running Ubuntu 18.04.


Solution

  • [ "$(<states.txt md5sum)" = "$(<states_copy.txt md5sum)" ]
    
    1. Use $(...) to get command output
    2. Remember to enclose $(...) inside "
    3. Bash test supports single = for string comparision, not double ==
    4. Redirect the files into md5sum using stdin and using < redirection.