The "opposite" of &> /dev/null

Paolo picture Paolo · Jul 16, 2014 · Viewed 7.6k times · Source

I use the following script to compare to folders:

if diff "/home/folder1/" "/home/folder2/" &> /dev/null ; then
    echo "Files in the two folders are the same"    
else
    echo "Files in the two folders are NOT the same"
fi

Is there a brief way of explaining what the "&> /dev/null" actually does, does it return a boolean value of true/false?

And my main question: What would the opposite be? I mean, say I want the "if diff" question to be "Is the content of the two folders NOT the same?"

Answer

Alfe picture Alfe · Jul 16, 2014

Your &> /dev/null redirects the stdout and stderr output to /dev/null, effectively suppressing all output of the process. The exit code of the program (the numerical "result", typically stating "success" (0) or "failure" (any other number)) is not influenced by that.

To reverse the condition (the diff), just insert an exclamation mark, stating a not operator:

if ! diff "/home/folder1/" "/home/folder2/" &> /dev/null ; then
...

The diff tool always terminates with exit value 0 if (and only if) it does not find any difference. If there is a difference, 1 is the exit value; if there is trouble (I/O error or such things), 2 is the exit value. The if of the shell interprets exit values of 0 to be true and all other values to be false (mind that, because this is quite the opposite of other programming languages!).