5

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?

1
  • FYI, this is not just true of functions -- just as return "$?" is default behavior for a function, exit "$?" is the implicit default behavior of a script. Commented Feb 17 at 18:41

2 Answers 2

8

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.)

Sign up to request clarification or add additional context in comments.

Comments

4

Did you try reading the manpage? It's in there.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.