Returning string values from a bash function

Ray Parrish crp at cmc.net
Sun Nov 22 04:56:21 UTC 2009


Ray Parrish wrote:
> Hello,
>
> I need to know how to return a string value from a bash function. I 
> tried using "return $VariableName", and got the error that return 
> expected a numeric value.
>
> So far I have been just operating on a variable declared before the 
> function call from within the function, i.e. global in scope to the 
> function itself, and that works, but I would like to know if I can 
> return the value of a variable that is local the the function.
>
> In other words I would like to be able to make a call like the 
> following, and have it work.
>
> SomeVariable=`MyFunction $Argument1`
>
> or
>
> SomeVariable=$( MyFunction $Argument1 )
>
> Is this possible in bash?
>
> Thanks, Ray Parrish
>   
Hello again,

I did some research, and wrote a little test script, and have discovered 
one method of returning strings from functions. See code below -

#!/usr/bin/env bash
#
# This script is to test different methods of returning a string from a 
function.
#
function ReturnString {
     echo "Success!"
}
#
Variable=`ReturnString`
echo "Variable contains $Variable"
exit

This worked great! Are there any other methods?

Thanks, Ray Parrish



-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list