Function Library
Another way of using functions is when you create more complicated scripts is to create functions that you place in a library so that you can source them from other scripts. These library functions can be called from a script using “source” or the “dot” command.
Here is a simple example. This is a script that is calling two different functions from library files. The source command is used to access the library file and then it is called by the function name.
#!/bin/bash
source pwd.lib
pwda
echo
source ps.lib
psu
This is what the library files look like, simple with no introductory line to indicate it is for a bash shell as you want to call them from the same shell.
ps.lib
psu ()
{
command ps -Hcl -F S f -u ${1:-$USER}
}
pwd.lib
pwda ()
{
command pwd -LP "$@"
}


{ 1 comment }
I know how to write functions and source them. If I was a beginner learning this I think this would be a bit hard to understand. I think there should be some more easier examples. Thanks for making a good effort!
Comments on this entry are closed.