What are the error exit values for diff? What are the error exit values for diff? shell shell

What are the error exit values for diff?


It depends on your diff command. Mine (GNU diffutils 3.0) says:

An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble. Normally, differing binary files count as trouble, but this can be altered by using the -a or --text option, or the -q or --brief option.


There maybe, or there may not be different error codes depending upon the version of diff you use. If I remember correctly, the standard BSD diff always returned an exit code of 0, 1, or 2.

However, the manpage isn't mapping out everything that diff might do, but the documentation you can use for using diff command. In a shell script, I want to know if the files matched (exit = 0) or didn't match (exit = 1). However, in my shell script, I also want to know that the diff command itself didn't work.

diff $file1 file2 > /dev/null 2>&1error=$?if [ $error -eq 0 ]then   echo "$file1 and $file2 are the same file"elif [ $error -eq 1 ]then   echo "$file1 and $file2 differ"else   echo "There was something wrong with the diff command"fi

Imagine if I was told that 2 meant the diff command failed, but a newer version of the diff command made a distinction between a file you can't read (exit = 2) and a missing file (exit = 3). Now, imagine if I did the following in an earlier version of the diff command, but $file2 didn't exist:

diff $file1 file2 > /dev/null 2>&1error=$?if [ $error -eq 2 ]then   echo "There was something wrong with the diff command"elif [ $error -eq 1 ]then   echo "$file1 and $file2 differ"else   echo "$file1 and $file2 are the same file"fi

In the above code, I checked for the error code of 2 and 1, but not 3. So, instead of detecting a missing file, I assume that the files match.

The manpage is trying to make sure that future upgrades to the OS don't cause most of your shell scripts to suddenly fail. It's why there was a separate awk and nawk command and a separate grep and egrep command.

*Updated as per comment by @chus.


In my case diff returned 127. Searched for it and found it in the tldp.org "Exit Codes With Special Meanings"

127 "command not found" - illegal_command - Possible problem with $PATH or a typo.

I used an incorrect path to diff. :)

Font: Advanced Bash-Scripting Guide.