cmp command compares two files and indicates whether they are identical or not.
If the two files are identical,
cmp returns nothing.
If the two files are distinct,
cmp generates a message on the standard output indicating the first element that differts between the two files.
/dev/null is a special system file that contains nothing.
Its size is always zero, even if one writes into it. It is convenient to use when redirecting.
cat `< /dev/null >` file
creates a file
file of size zero.
cat f1 > /dev/null
redirects the standard output to
/dev/null which will then contain nothing.
cmp f1 f2 > /dev/null
redirects the possible error message to /dev/null.
error code return¶
bash lets you know if a command ran with success or not with the variable
$? (see the variables section).
If a command has been executed normally,
$? is 0, and not 0 otherwise
(sometimes this value can have a meaning).
cmp f1 f1 echo $? cmp f1 f2 echo $? cmp f1 f2 > /dev/null echo $?
It is possible to use logical or or and with a second command depending on the result of the first.
The logical or writes
||, while the logical and writes
Like in C, the second part of the logical and is not executed if the first part is true, while with a logical or the second part is executed only if the first part is false.
cmp f1 f1 > /dev/null && echo the two files are identical cmp f1 f2 > /dev/null || echo the two files are not identical cmp f1 f1 > /dev/null && echo the files are identical || echo the files are not identical cmp f1 f2 > /dev/null && echo the files are identical || echo the files are not identical