Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use && with my own functions?

Tags:

bash

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?

like image 665
Rupert Madden-Abbott Avatar asked Nov 29 '22 04:11

Rupert Madden-Abbott


2 Answers

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

like image 154
nos Avatar answered Dec 06 '22 07:12

nos


In bash a return value of 1 indicates an error.

Try return 0

like image 23
blahdiblah Avatar answered Dec 06 '22 07:12

blahdiblah