triple fault
triple fault

Reputation: 14146

Getting function returned value

Suppose I have a function "func" which gets two arguments "a" and "b" and returns something. For some reason I can't manage echo what I get from the function.

This is what I tried:

value=$(func $a $b)
echo $value

Upvotes: 2

Views: 60

Answers (1)

user2404501
user2404501

Reputation:

Since you didn't show your function definition, this is only a guess, but I'm pretty confident it's a good one.

You need to understand that a function in shell fits into the language as if it was a command, i.e. a separate program, and the return keyword is analogous to exit. You can only return small integers, and the caller sees them in $?, just like the exit value of any other command.

Functions can also output a result to stdout by using echo (or any other command that prints to stdout), and then the result can be caputred with $(func args ...)

So you probably just need to change return to echo in your function.

Upvotes: 3

Related Questions