Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's does a bash function return when there is no “return” statement?

Is the return value of a bash function the status of the last executed command?

I wrote this test and it looks like it's so. I just want to verify. No one has asked this question before apparently and tutorials don't mention this.

Test program:

funa() {
  echo "in funa";
  true;
};

funb() {
  echo "in funb"
  false;
};

funa && echo "funa is true";    
funb && echo "funb is true";

Output when I run the program:

in funa
funa is true
in funb

Does anyone know the answer?

like image 591
bodacydo Avatar asked Dec 20 '22 17:12

bodacydo


1 Answers

Yes. Per man bash:

Shell Function Definitions

When executed, the exit status of a function is the exit status of the last command executed in the body. (See FUNCTIONS below.)

like image 179
nneonneo Avatar answered Jan 04 '23 05:01

nneonneo