Is a bash function a "function" or a "subroutine"?

Is a bash function a "function" or a "subroutine"?

Post by thompso » Mon, 12 Aug 2002 02:25:12



I can't seem to make a bash shell function work as I expect (darn
expectations anyway...) which is to return a value where the function is
called:

-----  cut here dr lector -----
# yes I know,  evil brace syntax
function bar()
{
   return 123

Quote:}

# None of these work
declare -i foo=bar
declare -i foo=bar()
declare -i foo=$(bar)
declare -i foo=$(bar)()
declare -i foo=$(bar())

# Gotta do this?
bar
declare -i foo=$?
-----  cut here dr lector -----

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by Bit Twist » Mon, 12 Aug 2002 02:39:44



> I can't seem to make a bash shell function work as I expect (darn
> expectations anyway...) which is to return a value where the function is
> called:

> -----  cut here dr lector -----
> # yes I know,  evil brace syntax
> function bar
> {
>    return 123
> }

Works for me

        function bar
        {
           return 123
        }
        bar
        echo $?

some light reading here
http://www.linuxdoc.org/LDP/abs/html/index.html

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by mjt » Mon, 12 Aug 2002 03:07:40


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message


> I can't seem to make a bash shell function work as I expect (darn
> expectations anyway...) which is to return a value where the function is
> called:

> -----  cut here dr lector -----
> # yes I know,  evil brace syntax

function bar()
{
   return 123

Quote:}

bar; rtnval=$?
echo $rtnval
...or...
bar; echo $?

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Michael J. Tobler: motorcyclist, surfer,  #    Black holes result
 skydiver, and author: "Inside Linux",     #   when God divides the  
 "C++ HowTo", "C++ Unleashed"              #     universe by zero

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9VVYmtTveLPAHcDIRAih5AJ0UGSekyPlfqlcJ9b2iLnk6KOpEGgCdFEzi
nRgEwhBJ4Dn4x7kx7YHTTUI=
=y52A
-----END PGP SIGNATURE-----

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by Floyd Davidso » Mon, 12 Aug 2002 03:56:10



>I can't seem to make a bash shell function work as I expect (darn
>expectations anyway...) which is to return a value where the function is
>called:
..
># Gotta do this?
>bar
>declare -i foo=$?

Generally it isn't necessary to use "declare -i".

  bar
  foo=$?

The above saves the return status for use later, and
for immediate use, one example is

  if bar
  then
    ...
  fi

Which will execute "..." when bar returns a 0 status.

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by thompso » Mon, 12 Aug 2002 07:43:38





>>I can't seem to make a bash shell function work as I expect (darn
>>expectations anyway...) which is to return a value where the function is
>>called:
> ..
>># Gotta do this?
>>bar
>>declare -i foo=$?

> Generally it isn't necessary to use "declare -i".

        According to OReilly book, this creates the variable as type integer.
Probably not necessary, but a hint to readers of what to expect.

Quote:>   bar
>   foo=$?

> The above saves the return status for use later, and for immediate use,
> one example is

Let's see if I've got this right : BASH "functions" are not callable using
the same paradigm as other scripting or programming languages ( except for
assembly).  In this respect they are really "subroutines".  They *do*
store a  numeric return value in a system variable which the caller then
accesses, but calling a BASH function and picking up the returned value
must be a 2-step process:

         1.     Call the function
        2.      Retrieve or assign the returned result.

In most other languages, the act of retrieving or assigning a function
result is performed by the same step that calls the function.  Not so in
BASH. This behaviour can confuse the BASH novice, who might hear the word
"function", and use their experience to provide an operational context of
what a function is, and how it is used.  To make life interesting, this
dichotomy is ignored by all documentation.

Quote:>   if bar
>   then
>     ...
>   fi

> Which will execute "..." when bar returns a 0 status.

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by Paul Colquhou » Mon, 12 Aug 2002 11:20:06


| I can't seem to make a bash shell function work as I expect (darn
| expectations anyway...) which is to return a value where the function is
| called:
|
| -----  cut here dr lector -----
| # yes I know,  evil brace syntax
| function bar()
|{
|    return 123
|}
|
|
| # None of these work
| declare -i foo=bar
| declare -i foo=bar()
| declare -i foo=$(bar)
| declare -i foo=$(bar)()
| declare -i foo=$(bar())
|
| # Gotta do this?
| bar
| declare -i foo=$?
| -----  cut here dr lector -----

The "return" value is a success/failure/error code, not the result
of the function.

Try this instead:

function bar()
{
        echo 123

Quote:}

--
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://www.tuxedo.org/~esr/faqs/smart-questions.html
 
 
 

Is a bash function a "function" or a "subroutine"?

Post by mjt » Tue, 13 Aug 2002 01:03:54


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message



> The "return" value is a success/failure/error code, not the result
> of the function.

> Try this instead:

> function bar()
> {
> echo 123
> }

bzzzzzzzzt!!! wrong ... and thanks for playing!  check the
"already posted answers" for a correct solution.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Michael J. Tobler: motorcyclist, surfer,  #    Black holes result
 skydiver, and author: "Inside Linux",     #   when God divides the  
 "C++ HowTo", "C++ Unleashed"              #     universe by zero

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9VoqbtTveLPAHcDIRAkD1AJ0ZH9WIU619KRqetIVrcN6qqogCJgCgglZf
0VlHGLqFXGnK0IGTWiO7Nz0=
=YOEg
-----END PGP SIGNATURE-----

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by Paul Colquhou » Thu, 15 Aug 2002 10:20:05


| -----BEGIN PGP SIGNED MESSAGE-----
| Hash: SHA1
| NotDashEscaped: You need GnuPG to verify this message
|

|
|> The "return" value is a success/failure/error code, not the result
|> of the function.
|>
|> Try this instead:
|>
|> function bar()
|> {
|> echo 123
|> }
|
| bzzzzzzzzt!!! wrong ... and thanks for playing!  check the
| "already posted answers" for a correct solution.
|

You have conveniently cut the original message where the poster was
trying to make one of the following pieces of syntax work as (they)
expected:

| # yes I know,  evil brace syntax
| function bar()
|{
|    return 123
|}
|
|
| # None of these work
| declare -i foo=bar
| declare -i foo=bar()
| declare -i foo=$(bar)
| declare -i foo=$(bar)()
| declare -i foo=$(bar())

When you do as I suggested, you get the following behaviour:

$ function bar()

Quote:> {
>         echo 123
> }

$ bar
123

$ declare -i foo=$(bar)

$ echo $foo
123

How does this not meet the original request?

--
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://www.tuxedo.org/~esr/faqs/smart-questions.html

 
 
 

Is a bash function a "function" or a "subroutine"?

Post by Paul Colquhou » Thu, 15 Aug 2002 10:40:05




|
|>>I can't seem to make a bash shell function work as I expect (darn
|>>expectations anyway...) which is to return a value where the function is
|>>called:
|> ..
|>># Gotta do this?
|>>bar
|>>declare -i foo=$?
|>
|> Generally it isn't necessary to use "declare -i".
|
|       According to OReilly book, this creates the variable as type integer.
| Probably not necessary, but a hint to readers of what to expect.
|
|
|>   bar
|>   foo=$?
|>
|> The above saves the return status for use later, and for immediate use,
|> one example is
|
|
|
| Let's see if I've got this right : BASH "functions" are not callable using
| the same paradigm as other scripting or programming languages ( except for
| assembly).  In this respect they are really "subroutines".  They *do*
| store a  numeric return value in a system variable which the caller then
| accesses, but calling a BASH function and picking up the returned value
| must be a 2-step process:
|
|        1.     Call the function
|       2.      Retrieve or assign the returned result.
|
| In most other languages, the act of retrieving or assigning a function
| result is performed by the same step that calls the function.  Not so in
| BASH. This behaviour can confuse the BASH novice, who might hear the word
| "function", and use their experience to provide an operational context of
| what a function is, and how it is used.  To make life interesting, this
| dichotomy is ignored by all documentation.

It depends on the use to which you will be putting the function.

If you are goung to be using it to return a boolean type yes/no response
to determine program flow, then user the "return" command at the end to
set the appropriate status value for the function.

If you are using it to compute a numeric or string value from inputs for
use in further computations, the use "echo" to set the output value.
(You should still set the "return" to a non-zero value in case of an error
condition, which you can check for as well as using the real answer.)

For example:

$ function bar
{
  echo 123

Quote:}

$ declare -i foo=$(bar)

$ echo $foo
123

--
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://www.tuxedo.org/~esr/faqs/smart-questions.html