Main Topics
Browse All TopicsI'm a Windows programmer by trade......
I have 2 books that both explain functions. Neither show how to assign a return function value to a variable. Either externally or within the same script. All examples I have found work the same as a Windows "subroutine"....returning no value. I think I'm close but don't have the correct syntax. Here's the pseudocode.
**************************
function getChars {
var1=$1
var2=$(var1%.*)
return var2
}
.....
var3=$(getChars abcdefg)
**************************
In my case the function and the "call" to it are all in the same script. Can someone help me with the "syntax".."tricks"..."do's
Thanks...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When used, a function should be seen like a normal
program ie, like normal programs, a function can only
return an "exit" value which is an _integer_.
But unlike normal programs, variables defined in
function stay "alive" after the function returns so
you can use $var2 after calling getChars :
function getChars
{
var2=$1
}
getChars abcdefg
echo $var2
Stephane
You can sorta do what you want by:
function getChars {
var1=$1
var2=$(var1%.*)
echo "$var2"
}
var3=$(getChars abcdefg)
ie: if the function output something, then you can capture it in much the same way as if you are calling an external script.
return is used for returning a status value. So in the above example, if something failed, you'd return a non zero value, eg:
function getChars {
var1=$1
var2=$(var1%.*)
echo "$var2"
if [[ sometest ]]
then
return 0 # Succesful
else
return 1 # failed
fi
}
var3=$(getChars abcdefg) || exit $?
or alternatively
var3=$(getChars abcdefg)
if [[ $? -ne 0 ]]
then
echo "The function failed"
exit 1
fi
Business Accounts
Answer for Membership
by: ahoffmannPosted on 2003-03-05 at 06:32:51ID: 8071966
you cannot return variable (by reference) in shell scripting, you just can return values (either integer or strings).
If you need to "return" more than one value, you need to use global variables, or pack the resuklts together in the function and parse it at the calling place.