Unexpected(?) return value inside if-else

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I've stumbled over this weird behavior with a wrong return value inside an if-else, I think the best way to explain this is showing a short script...

This script doesn't do anything, it's just an easy way to explain what I'm seeing: https://pastebin.com/vt0WStHV

a() returns 0, so both bash and dash will print a() = 0.

The "if" on b is of course false, so it goes to the else and returns the return value of a(). Dash will print "b() = 1" and bash will print "b() = 0".

If you comment out lines 6,7,8 and 10 bash and dash will print "b() = 0".

This is just a sample code and doesn't do anything useful. And it would be easy to workaround this issue changing the code. And I'm very likely doing something non POSIX-compliant with return `something`. But I don't know what it is and I'm very curious to know what is going on. Please take a look, thank you.
________________________
a(){
        return `echo x |grep -q x`
}

b(){
        if echo x |grep -q y ;then
                return 100
        else
                return `a`
        fi
}

a
echo "a() = $?"
b
echo "b() = $?"
_________________________

--

Lauro Costa
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux