Prajwal
Prajwal

Reputation: 673

bash function returns always true

I have a shell script

function fun1(){
    return 0;
}

function fun2(){
    return 1;
}

if  fun1  && fun2  ;then 
echo "success"
else 
echo "failure"
fi

which works fine.. I need to wrap this up in double brackets. I am trying to do like this.

if  [[ fun1 ]]  && [[ fun2 ]] ;then 
echo "success"
else 
echo "failure"
fi

However it always returns success regardless of the return values of the functions..

Can anybody tell me where am I going wrong? However If if run witho

Upvotes: 0

Views: 398

Answers (1)

Kent
Kent

Reputation: 195239

if  fun1

means that, use the result of function fun1 as if condition. So it worked as expected.

if  [[ fun1 ]]

Once you used [[ or [, you are calling a command test (or a new powerful command test for [[), so the fun1 would be argument for the test command. Your checking thus becomes: "if fun1 is not an empty string". Thus, it returns always true.

Upvotes: 3

Related Questions