Search code examples
bashcmp

Bash, how to print out that files are the same when using cmp


cmp file1 file2 does nothing when the files are the same. So how do I print out that files are the same in shell script?


Solution

  • The exit status of cpm is zero if the files are identical, and non-zero otherwise. Thus, you can use something like

    cmp file1 file2 && echo "Files are identical"
    

    If you want to save the exit status, you can use something like the following instead:

    cmp file1 file2
    status=$?
    if [[ $status = 0 ]]; then
        echo "Files are the same"
    else
        echo "Files are different"
    fi