You want to use a function and you need to get some values into the function. How do you pass in parameters? How do you get values back?
You don’t put parentheses around the arguments like you might expect from some programming languages. Put any parameters for a bash function right after the function’s name, separated by whitespace, just like you were invoking any shell script or command. Don’t forget to quote them if necessary!
# define the function: function max () { ... } # # call the function: # max 128 $SIM max $VAR $CNT
You have two ways to get values back from a function. You can
assign values to variables inside the body of your function. Those
variables will be global to the whole script unless they are explicitly
declared local
within the
function:
# cookbook filename: func_max.1 # define the function: function max () { local HIDN if [ $1 -gt $2 ] then BIGR=$1 else BIGR=$2 fi HIDN=5 }
For example:
# call the function: max 128 $SIM # use the result: echo $BIGR
The other way is to use echo or printf to send output to standard output. Then you must invoke the function inside a $(), capturing the output and using the result, or it will be wasted on the screen:
# cookbook filename: func_max.2 # define the function: function max () { if [ $1 -gt $2 ] then echo $1 else echo $2 fi }
For example:
# call the function: BIGR=$(max 128 $SIM) # use the result echo $BIGR
Putting parameters on the invocation of the function is just like calling any shell script. The parameters are just the other words on the command line.
Within the function, the parameters are referred to as if they
were command-line arguments by using $1
, $2
,
etc. However, $0
is left alone. It
remains the name by which the entire script was invoked. On return from
the function, $1
, $2
, etc. are back to referring to the
parameters with which the script was invoked.
Also of interest is the $FUNCNAME array. $FUNCNAME all by itself references
the zeroth element of the array, which is the name of the currently
executing function. In other words, $FUNCNAME is to a function as
$0
is to a script, except without all
the path information. The rest of the array elements are what amounts to
a call stack, with “main” as the bottom or last element. This variable
only exists while a function is executing.
We included the useless variable $HIDN
just to show that it is local to the
function definition. Even though we can assign it values inside the
function, any such value would not be available elsewhere in the script.
It is a variable whose value is local to that function; it comes into
existence when the function is called, and is gone once the function
returns.
Returning values by setting variables is more efficient, and can handle lots of data— many variables can be set—but the approach has its drawbacks. It requires that the function and the rest of the script agree on variable names for the information hand-off. This kind of coupling has maintenance issues. The other approach, using the output as the way to return values, does reduce this coupling, but is limited in its usefulness—it is limited in how much data it can return before your script has to spend lots of effort parsing the results of the function. So which to use? As with much of engineering, this, too, is a trade-off and you have to decide based on your specific needs.
3.19.31.73