Re: Unexpected(?) return value inside if-else

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

 



On Mon, Feb 26, 2018 at 02:30:15PM -0300, Lauro Costa wrote:
> Subject: Unexpected(?) return value inside if-else
> 
> 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() = $?"
> _________________________

You probably misunderstood what process substitution (``) is: a simple
string substitution.  In your examples all it does is execute a command
but substitute nothing.

Your examples reduce to:

$ dash -c 'b() { false; `true` return; }; b; echo $?'
1
$ bash -c 'b() { false; `true` return; }; b; echo $?'
0

Without process substitution both shells return 1:

$ dash -c 'b() { false; return; }; b; echo $?'
1
$ bash -c 'b() { false; return; }; b; echo $?'
1

What happens is that bash returns the return value of the process
substitution on the return command and dash the return value of the last
command.  I do not know if there is a specification mandating one or the
other behaviour but that's bash's behaviour that I find surprising.
--
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