0

The script is test.sh.

#!/bin/sh

if cmp -s file.a file.b; then echo diff else echo same fi

When checking the exit code from cmp -s I see:

When file.a and file.b are different.

cmp -s file.a file.b
echo $?
1

When file.a and file.b are the same.

cmp -s file.a file.b
echo $?
0

Why then does the conditional statement seem to operate in the reverse?

When file.a and file.b are different.

./test.sh
same

When file.a and file.b are the same.

./test.sh
diff

This isn't the first time I've seen this. I must be misunderstanding how the if statement interprets the exit code, or maybe it isn't the exit code at all?

Appleoddity
  • 141
  • 1
  • 1
  • 9

0 Answers0