All programming languages have a feature called a function. These are blocks of code that can be used in a script.
A function usually has code that does a particular task, and can be used in other scripts. It is like a script within a script.
Functions provide a method of defining a computation that can be executed later.
In fact many scripts are just a collection of functions.
Functions are available in bash, but are somewhat limited, compared to other languages.
They can be used either from the command line, or in a script.
There are 2 ways to define a function in a script:
function_name () {
commands
}
function_name () { commands; }
Since most functions consist of several lines of code, the first method above is preferred - it is easier to read. The executable code lies in the block delimited by curly braces { }.
Once a function is defined, we can use by just calling its name:
A simple function:
fn () {
echo "Hello World!"
}
fn
Hello World!
Here the function fn was first defined, then used immediately. It can also be used at any time later in the same script.
An important thing to note about variables in functions is their scope.
Variables in bash are global by default, but can be restricted when used in a function.
That means if you define a variable outside a function, it is also availabe inside the function.
#!/usr/local/bin/bash
ThisVar="Forty-two"
echo I have $ThisVar dollars outside ...
function PayMore {
echo Now in the 'PayMore' bar ...
echo I came in here with $ThisVar dollars!
ThisVar="two bucks"
}
PayMore
echo Not in the bar anymore ...
echo But now I only have $ThisVar!
Running this script costs some $....
func.sh I have Forty-two dollars outside ... Now in the PayMore bar ... I came in here with Forty-two dollars! Not in the bar anymore ... But now I only have two bucks!
Aside from being costly, this script shows how the variable ThisVar was altered inside the function, but kept that value when the function was completed.
If you want to use a variable that only a function knows about, we have to define it using local.
Try the local bar instead:
#!/usr/local/bin/bash
clear
ThisVar=Forty-two
echo I have $ThisVar dollars outside ...
function PayMore {
echo Now in the 'PayMore' bar ...
echo I came in here with $ThisVar dollars!
ThisVar="two bucks"
}
PayMore
echo Not in the bar anymore ...
echo But now I only have $ThisVar!
echo
function WhosOnFirst {
echo I\'m going to try the local bar instead...
echo "Heading in with $ThisVar ..."
local ThisVar="Seventy bucks"
echo "I won a bet with another guy! I'm $ThisVar richer!"
}
WhosOnFirst
echo "But when I leave, I still have only $ThisVar!"
echo
I have Forty-two dollars outside ... Now in the PayMore bar ... I came in here with Forty-two dollars! Not in the bar anymore ... But now I only have two bucks! I'm going to try the local bar instead... Heading in with two bucks ... I won a bet with another guy! I'm Seventy bucks richer! But when I leave, I still have only two bucks!
Note we had to escape the single quote (apostrophe) in the first I'm otherwise bash would complain about a missing closing quote. But in the 2nd instance, double-quotes were used around the whole string.
Sometimes a function requires some specific input, such as an array, filename, or numerical data. If you know this ahead of time, you can pass that information directly to the function as parameters.
We pass the values to the array by including them after the function name.
Here is a simple script that multiplies 2 integers together...
Remember: bash only knows about integers.
#!/usr/local/bin/bash
# multiply 2 numbers
clear
function multi {
if [ $# -ne 2 ] # are there 2 parameters
then
echo Incorrect input. Need 2 integers
else
echo $[ $1 * $2 ]
fi
}
multi $1 $2
... and is used like this:
multi 57 313which should result in
17841
That works fine, but many times a function has a value or variable that we would like to use in the main script.
Your function may be calculating a value after being given some input values. This is how that value can be used.
Here is a simple script that multiplies 2 integers:
#!/usr/local/bin/bash
# multiR.sh
# multiply 2 numbers & return result
clear
echo "What is $1 * $2"
echo
function multiR {
if [ $# -ne 2 ] # are there 2 parameters?
then
echo Incorrect input. Need 2 integers
else
MyResult=$[$1 * $2]
fi
}
multiR $1 $2
echo Ah, so it\'s $MyResult
It's used like this:
multiR.sh 2443 5894which should result in
What is 2443 * 5894 Ah, so it's 14399042Functions 102