Running this prints out both statements
#!/bin/bash
echo "Hello" && echo "World"
However, running this only prints out "Hello"
#!/bin/bash
message() {
echo "Hello"
return 1
}
message && echo "World"
Why doesn't this work and how can I change my script so that it does?
An "exit" value of 0 means success while anything else means failure. and the && operator doesn't execute the right hand side if the left hand side fails (that is if it returns non-zero)
So change the return 1
to return 0
In bash a return value of 1
indicates an error.
Try return 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With