Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning a value from a function in a shell script

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
have a shell script in which I have implemented functions. How do I return
a value back to the calling function. Here is an example.

#! /usr/bin/ksh
function testfunc
{
typeset -i a=1;
a=$((a+1))
return $a
}
echo "Now calling testfunc"
testfunc
echo "Now back"
echo $a

However I am not able to get the value of a which should now be 2. Can
someone suggest a solution. As an addendum I would like to know how to
return an entire array back to the calling function.


 
Hi,

If you say 'return 2' does the value get passed back then?
Mike
michael.j.lacey@ntlworld.com
 
Here you go, the trick is that a return value from a program, a shell script or a function is returned in $? so you have to set a variable to that value as the first operation after calling the function (or whatever).

Mike
[tt]
#!/usr/bin/ksh

function testfunc
{
typeset -i a=1;
a=$((a+1))
return $a
}
echo "Now calling testfunc"
testfunc
b=$?
echo "Now back"
echo $b
[/tt]


Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top