1

For example

function fff(){
    true && (return)
    echo 1
}
fff

The output is 1.

1 Answer 1

1

Clearly the fff reaches till its end and that's why 1 is being printed. The point is that return is executed in a subshell as you have placed it inside parenthesis. You should have done

true && return

instead of

true && (return)

Anything placed inside () is executed using a subshell and after the commands in the subshell finishes the control is passed back to the function fff and the subsequent command echo 1 is executed in this case.

Below script will give you the right output

#!/bin/bash
function fff(){
 true && return 255
echo 1
}
fff 
echo $? # Will give you 255 as output

Mind that return value greater than 255 will be wrapped around ie you will get (value modulo 255) - 1. For example if you return 259 you will get 3 ie (259%255)-1.

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

1 Comment

@BigShield : Thankyou for accepting the answer. Also, see a bit of explanation I've just added.

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.