Running this output prints both statements
#!/bin/bash echo "Hello" && echo "World"
However, when you run this file, only "Hello" is displayed.
#!/bin/bash message() { echo "Hello" return 1 } message && echo "World"
Why does this not work and how can I change my script so that it does?
An exit value of 0 means success, and something else means failure. and the && operator does not execute the right side if the left side fails (that is, if it returns a nonzero value)
So change return 1toreturn 0
return 1
return 0
return 1(or anything else that is not 0) means falsebefore bash, which means that the second command will not be called.
false
return 0 ( true) (, || ;.)
true
||
;
, bash , && || " " , " " ". a && b " b , a ", " " ( true 0, false 1), , .
&&
a && b
b
a
bash 1 .
1
& & , , 0
return message().
message()