1

I'm looking at this code:

if diff file1 file2 > /dev/null; then echo "same files!" ` else ..... 

Except isn't diff evaluated false if its output is directed to /dev/null?

3
  • 4
    The value of diff file1 file2 > /dev/null is the return value of diff. Why would you expect diffs return value to be different based upon where you redirected its output? Commented Sep 27, 2013 at 2:32
  • I m not sure. The issue is diff file1 file2 should just return 0 if the files are the same right? So I don't understand why they redirected the output to dev/null Commented Sep 27, 2013 at 2:37
  • This code is only concerned whether the files are the same or not. diff outputs all the differences. If you don't want to see the differences but just want to know there is a difference, you send the output to /dev/null. That's where you send output you don't care about. The return value is separate, and not the "output" of diff. Commented Sep 27, 2013 at 2:39

3 Answers 3

4

The simplest way to illustrate the difference is by example:

$ if diff file1 file2 > then echo The same > else echo Different > fi 2c2 < bb --- > ee Different $ if diff file1 file2 > /dev/null > then echo The same > else echo Different > fi Different $ diff -u file1 file2 --- file1 2013-09-26 19:44:06.000000000 -0700 +++ file2 2013-09-26 19:44:19.000000000 -0700 @@ -1,4 +1,4 @@ aa -bb +ee cc dd $ 

Without sending the output to /dev/null, you see both the standard output of diff and the appropriate actions from testing the exit status of diff. When you send the output of diff to /dev/null, you don't see the diff output at all, but you do still get the appropriate action from testing the exit status of diff. If you are writing a script, you often do not want the user to see the actual differences; it is sufficient to know that the files are different.

There are other tools that can be used for that job; in particular, cmp -s file1 file2 returns just an exit status indicating whether the files are the same or different (and it can be used on binary files, not just text files). You don't need the I/O redirection with cmp. However, it is legitimate to use diff and to hide the differences.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks also, how did you format the code to be nice like that?
Years of practice :) In the edit box, type the code the way you want it to look (or, better, prepare it in an editor somewhere, then copy and paste to the edit box). Avoid using any tabs in the input; they mess up the layout. Then select the material that is to be the code and press the {} button above; that indents the selected material by 4 spaces, and the MarkDown language then treats it as 'code' and formats it in fixed-width font with a greyed background. You can also use the help above the edit box. Or you can just type the code with four spaces at the start of each line.
Saying diff -q file1 file2 obviates the need of redirecting to /dev/null.
@devnull: Yes, you're right if your diff has extensions that the POSIX version of diff does not, and specifically if it supports the -q option (which the POSIX version does not).
@JonathanLeffler Right, but as you already mentioned cmp is probably better in that case :)
3

No, the shell is testing against the return value of diff, not what it wrote to the standard output. Return values are different. They are a status code returned by a program when it exits. So, it doesn't matter where you directed the output (if at all). And no /dev/null does not have a return value. It's not a program that you run - it's just somewhere to send data that you don't want to store.

7 Comments

Wouldn't you call that "somewhere" a device?
That's one of the last things I would call /dev/null personally.
@ErikeE yes I would technically, but I like to think of it as a black hole. More of a special file that ignores any data written to it.
So then technically I don't need to redirect the output to dev/null?
Don't forget /dev/null is also an empty file when it is read (so it is an empty data source and infinitely capacious data sink). And yes, /dev/null is a device; that's why it lives in the /dev directory, along with most of the other devices on the system.
|
1

/dev/null doesn't have a return value since it's not a binary that is executed. It is simply a place to redirect data when you don't care about the output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.