Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been toying around with makefiles and bash scripts and I'm interested in this:

Is it possible to get a boolean value from a diff(or something similar) so that I can use it in a bash script to evaluate an if statement(so that the user will not see the actual diff executing)?

share|improve this question

If all you need is a byte-by-byte comparison, use cmp:

if cmp -s "$a" "$b"; then
    echo Same
    echo Differ

This avoids wasting time for diff's difference finding algorithm.

share|improve this answer


if diff "$file_a" "$file_b" &> /dev/null ; then
    echo "Files are the same"
    echo "Files differ"
share|improve this answer

The manual is not clear on the return codes. However, diff should return always 0 when you compare two identical files.

diff -a $file1 $file2 > /dev/null 2>&1

if [ $? -eq 0 ]
share|improve this answer
"Exit status is 0 if inputs are the same, 1 if different, 2 if trouble." (GNU diffutils 3.2) - Seems fairly clear to me. – grawity Feb 20 '12 at 23:02
diffutils 2.9.19-4065 completely lacks of a "return value" section – ziu Feb 21 '12 at 11:50
Be careful ! This construction will fail if set -e is active and files differ. Better use if diff ... construction. The -e flag ("Exit immediate on error") is handy when all error cases are not yet handled or covered and the script is harmful. – levif Sep 20 '15 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .