1

Is there an elegant way to compare exit codes of two functions in bash? For example

b ()
{
    local r=$(rand -M 2 -s $(( $(date +%s) + $1 )) );
    echo "b$1=$r";
    return $r;
} # just random boolean    
b1 () { b 1; return $?; } # some boolean function
b2 () { b 2; return $?; } # another boolean function ( another seed )

I'd like to use something like this

if b1 == b2 ; then echo 'then'; else echo 'else'; fi

but stuck with this "not xor" implementation

if ! b1 && ! b2 || ( b1 && b2 ) ; then echo 'then'; else echo 'else'; fi

And speaking more generally, can one compare exit codes of two functions arithmetically and use that comparison in if statement?

8
  • Just to clarify, what do you expect this code b 1; return $?; to do? Since the result of running b 1 is not saved in a variable, what are you hoping to accomplish by running it? Separately, what do you expect the value of $? to signify? Commented May 23, 2016 at 22:16
  • I expect b 1; return $?; returns result of b 1, i.e. zero or one, because $? is a variable that stores exit code of last executed command. Yes, i do understand difference between stdout and function result; Commented May 23, 2016 at 22:38
  • Shell functions don't have return values; they have exit statuses, with well-defined semantics: 0 means success, any other non-zero value means failure. Commented May 23, 2016 at 23:03
  • Ahhh, i see why this question. It's just a sample. Actually b1 and b2 are some test functions like IsSomeAssertion and IsAnotherAssertion. And i want to know if both of them are false or true at the same time. Just now i end up with this solution: if (( $(b1)$? == $(b2)$? )) ; then echo 'then'; else echo 'else'; fi but it works only if b1 and b2 don't put anything to stdout. Commented May 23, 2016 at 23:08
  • @chepner yes, i meant return status. BTW, i never used term "return value" Commented May 23, 2016 at 23:14

1 Answer 1

1

To compare exit codes of b1 and b2:

b1; code1=$?
b2; code2=$?
[ "$code1" -eq "$code2" ] && echo "exit codes are equal"

In shell, statements like b1 == b2 cannot stand alone. They need to be part of a test command. The test command is commonly written as [...]. Also, in shell, = (or, where supported, ==) are for string comparison. -eq is for numeric comparison.

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

2 Comments

Thank you for good explanation. By the way, as far as i understand, in (( )) construction == is for numeric comparison.
Yes, inside ((...)), = is assignment and == is a test for equality. The construct ((...)) offers bash's full arithmetic capabilities. This, however, can lead to unpleasant surprises. Another difference is [...] is POSIX and therefore portable but ((...)) is not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.