2

In bash, how can I directly return the return value returned by calling a function (sry, I don't know how to express this better). Example:

foo() {
  echo "$1"
  return 1
}

bar() {
  return foo 1
}

bar

If I do this, bash complains that a numeric parameter for the return statement is needed.

EDIT

I updated my example to better express the real problem. No only do I want to return the return code, I also want to pass a value to the function first... not sure if this is actually doable.

0

4 Answers 4

3

The only thing you can return from a shell script or a shell function is a numeric error code.

However, you can print some text to standard output in the function (or separate script, it's the same) using echo, cat, etc., and then capture the output, using bacticks syntax or $(...) syntax. Passing parameters to shell functions works the same way as passing parameters to scripts:

http://www.freeos.com/guides/lsst/advance01.html

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

4 Comments

backticks are deprecated - use $(...) instead.
I don't know who said bacticks are deprecated but yes, perhaps the syntax with dollar is better. I updated the answer with that.
I thought I've seen it in the manpage or the advanced bash scripting guide, but can't find it again. But I can explain the reason behind it: The $(...) syntax is easily nestable, and with some fonts, it is hard to distingish apostrophs from backticks.
Yes, I agree, it's all true. But when I work interactively I always use bacticks, it's just faster. In a script $(...) syntax is preferable, definitely.
1

In these modifications of your example, I change the argument to foo to make it easier to distinguish the result of one from the other.

foo() {
  echo "$1"
  return 1
}

bar() {
  return "$(foo 2)"
}

bar
echo "$?"

The preceding will output "2". The echo in foo is used as the return value of bar. The range of values that return (and exit) can handle is 0 to 255.

foo() {
  echo "$1"
  return 1
}

bar() {
  foo 2
  return "$?"
}

bar
echo "$?"

The second version will first output 2 since that's what foo does then a 1 will be output since that's the return value of bar having been propagated from the return value of foo.

2 Comments

+1 I'll guess I stick to the second version because AFAIK the call "$(foo 2)" will execute foo in a subshell which is not what I want.
@OliverWeiler: Well, you'll have to use the first version if you need to pass a result that is a string or an integer outside the range of 0 - 255.
1

This should work: you can only return a number in bash.

foo() {
  return 1
}

bar() {
  foo
  return 1
}

bar

Comments

1

You can simply return the return code of the last call $?:

foo() {
  echo "$1"
  return 1
}

bar() {
  foo "bla"
  return $?
}

bar

1 Comment

return $? isn't necessary. The exit code of a function is that of the last command in the given compound command. f() { g; }; g() { false; }; f; echo $?. Only if there were more commands would you have to save the exit code and return it explicitly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.