The error occurs because the return statement in Bash expects a numeric exit status (0 for success, 1-255 for failure), but you are trying to return the result of a conditional expression ([[ "$a" == "b" ]]), which is a boolean (true or false), not a number.
In Bash, [[ ... ]] is a conditional expression that returns true (0) or false (1) as an exit status. If you want to return the result of the comparison, you can directly use the exit status of the [[ ... ]] expression. However, return expects a numeric value, so you need to ensure you are returning a number.
Here’s the corrected version:
bashmy_test() { a='c' if [[ "$a" == "b" ]]; then return 0 # true else return 1 # false fi } my_test
Or, more concisely, since [[ ... ]] already sets the exit status:
bashmy_test() { a='c' [[ "$a" == "b" ]] } my_test
In this version, the function will return the exit status of the [[ ... ]] command, which is 0 (true) if the condition is met, and 1 (false) otherwise.
return in Bash must be followed by a number (0-255).[[ ... ]] is a command that returns 0 (true) or 1 (false) as its exit status.if and explicitly return 0 or return 1.[[ ... ]]).Vibe can make mistakes. Check answers. Learn more