Operating System - HP-UX
1834460 Members
2712 Online
110067 Solutions
New Discussion

Re: return value from shell function

 
SOLVED
Go to solution
andi_1
Frequent Advisor

return value from shell function

Hi guys,

Is it possible to return values within functions in the shell code?

e.g.

function A
{
retFromB = B

echo $retFromB //should display 5
}

function B
{
i=5

return $i
}

Thank you!
2 REPLIES 2
Tom Danzig
Honored Contributor
Solution

Re: return value from shell function

Yes. YOu could do something like:

#!/usr/bin/sh

FuncA(){
FuncB
return $1
}

FuncB(){
return 5
}

FuncA
echo $?
Sachin Patel
Honored Contributor

Re: return value from shell function

Hi Leon,
Yes you can do that

For example

increment()
{
sum='expr $1 + 1'
return $sum
}

increment 5
echo $?
echo $sum

Retun value is stored in $? also main script knows the variable $sum.

Sachin Patel
Is photography a hobby or another way to spend $