Understanding Functions

by mike on July 5, 2011

A function can help you modularize your script.  Because the function is executed in the context of the same shell, it does not create a new child process, this makes functions faster as they access the information in RAM.  This also will save on resources as you use functions.  Functions are a script within a script which can be defined by the user and stored in memory, allowing you to reuse the function repeatedly.  This also provides  a modular aspect that allows you to debug one function at a time be disabling functions.

Think of functions as miniature shell scripts, or containers inside a shell script where each container functions within itself to provide resources for the larger script.  One big difference between a shell script and a function is that a new instance of the shell is started for a shell script but the function runs in the current shell.

Bash Shell Functions

Several issues to note concerning functions.   The shell will look for aliases first, then functions, then builtin commands and finally executables.    The function must always be defined before it is used.  The function runs within the environment that is current and shares variables with the script.  You can view the current functions with the declare command.  List the whole function with this:

declare -f

Of course if you do not have a function it will not show anything.  So add a function at the command line so you can list functions that are available.

dfh() { df -h; }
declare -f
dfh ()
df -h

declare -F
declare -f dfh
Filesystem            Size  Used Avail Use% Mounted on
/dev/simfs            2.0G  801M  1.3G  40% /
none                  128M  4.0K  128M   1% /dev

Use the “-F” option to just view the names of the current functions.
declare -F
declare -f dfh
declare -f duh

Arguments passed to the function are used as positional parameters within the function.  If functions are stored in a separate file they can be accessed with the source or dot command.

Functions can be created several different ways.  The Bourne shell originally used the first method where function is the name of the function followed by empty parentheses.  The Korn shell introduced the second method using the term  “function” as a keyword, followed by the function name  and then describing the function.

function () { command ; command; }
function function_name { command ; command; }
function function_name () { command ; command; }

Functions can be unset with:
unset -f function_name

unset -f dfh
declare -f

Now this function should not longer be listed as it is not in memory.


Zach July 5, 2011 at 2:50 pm

Great post! Learned a bit by reading this. I like to use ‘declare -r’ in ~/.bashrc to enforce HISTFILE etc..

Looking forward to your next post!

luky July 6, 2011 at 4:46 am

What are “builtin commands”?

Andrew July 6, 2011 at 3:34 pm

A builtin command is a command that is built into the shell so that the shell does not fork a new process.

Comments on this entry are closed.

Previous post:

Next post: