How can I get the return value of a function in a shellscript?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I get the return value of a function in a shellscript?
How can I get the return value of a function in a shellscript?
If I use `` or $(), I get everything that is printed when the function is called.
But I want what I put in the return() statement.
I would like to be able to do something like this for example:
Code:
#! /bin/bash
foo()
{
echo "$0 says: hello $1"
zed=42;
return 1;
}
var=`foo`
echo $var
#and I would like to get "1" as output of the script
For normal programs, I can use "$?", but this doesn't seem to work here.
I want to use personalized return values like 512, 47, strings or whatever.
Well, I can always use normal variables since they seem to be kept the same everywhere in the script unlike C/C++.
I'd be interested too.
I only know basic grep, sed and awk command-line usage and I currently have no idea how to write a script doing this. Maybe it's easier with perl.
Ok, thanks, it works now.
But return only takes numeric values apparently.
Quote:
EXIT STATUS
The value of the special parameter '?' shall be set to n, an unsigned decimal integer, or to the exit status of the last command executed if n is not specified. If the value of n is
greater than 255, the results are undefined. When return is executed in a trap action, the last command is considered to be the command that executed immediately preceding the trap
action.
I should read the man more often.
[I even started reading the source code of true and false. 82 lines for true.c and 2 for false.c ^^.]
So there is no way to access the return value directly as in C?:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.