FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-22-2009, 02:31 AM
Ray Parrish
 
Default Returning string values from a bash function

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

--
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



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-22-2009, 03:56 AM
Ray Parrish
 
Default Returning string values from a bash function

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



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-22-2009, 05:48 AM
Cameron Hutchison
 
Default Returning string values from a bash function

Ray Parrish <crp@cmc.net> writes:

>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.

You use "echo".

eg.

foo()
{
echo "result"
}

bar=$(foo)


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-22-2009, 06:52 AM
James Michael Fultz
 
Default Returning string values from a bash function

* Ray Parrish <crp@cmc.net> [2009-11-21 20:56 -0800]:
> 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?

printf can be used more safely than echo when printing the value of
a variable since echo may try to interpret specific sequences as options
or escape sequences.

function ReturnString {
retval="-n"
# echo will interpret '-n' as an option
echo "$retval"
# printf prints the string as-is
printf '%s
' "$retval"
}

You can also set the value of a global variable to be checked after
a function execution.

# set to null value
ReturnValue=

function MakeMagicHappen {
ReturnValue=xyzzy
}

MakeMagicHappen

if [[ "$ReturnValue" == "xyzzy" ]]; then
: magic happens
fi

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 05:02 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org