# Creating a Library - Shell Scripting

It’s easy to see how functions can help save typing in a single script, but how about if you just happen to use the same single code block between scripts? It would seem to not help if you have to define the same function in each script, only to use it one time in each script.

There’s a solution for that problem! The bash shell allows you to create a library file for your functions, then reference that single library file in as many scripts as you need to.

The first step in the process is to create a common library file that contains the functions you need in your scripts. Here’s a simple library file called myfuncs that defines three simple functions:

$cat myfuncs # my script functions function addem { echo$[ $1 +$2 ]
}
function multem {
echo $[$1 * $2 ] } function divem { if [$2 -ne 0 ]
then
echo $[$1 / $2 ] else echo -1 fi }$

The next step is to include the myfuncs library file in your script files that want to use any of the functions. This is where things get tricky.

The problem is with the scope of shell functions. Just as with environment variables, shell functions are only valid for the shell session in which you define them. If you run the myfuncs shell script from your shell command line interface prompt, the shell creates a new shell, and runs the script in that new shell. This will define the three functions for that shell, but when you try to run another script that uses those functions, they won’t be available.

This applies to scripts as well. If you try to just run the library file as a regular script file, the functions won’t appear in your script:

$cat badtest4 #!/bin/bash # using a library file the wrong way ./myfuncs result=addem 10 15 echo "The result is$result"
$./badtest4 ./badtest3: addem: command not found The result is$

The key to using function libraries is the source command. The source command executes commands within the current shell context instead of creating a new shell to execute them. You use the source command to run the library file script inside of your shell script. This makes the functions available to the script.

The source command has a shortcut alias, called the dot operator. To source the myfuncs library file in a shell script, all you need to do is add the following line:

. ./myfuncs

This example assumes that the myfuncs library file is located in the same directory as the shell script. If not, you’ll need to use the appropriate path to access the file. Here’s an example of creating a script that uses the myfuncs library file:

$cat test14 #!/bin/bash # using functions defined in a library file . ./myfuncs value1=10 value2=5 result1=addem$value1 $value2 result2=multem$value1 $value2 result3=divem$value1 $value2 echo "The result of adding them is:$result1"
echo "The result of multiplying them is: $result2" echo "The result of dividing them is:$result3"
$./test14 The result of adding them is: 1514 The result of multiplying them is: 50 The result of dividing them is: 2$

The script successfully uses the functions defined in the myfuncs library file.