Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book


A shell function (tcsh does not have functions) is similar to a shell script in that it stores a series of commands for execution at a later time. However, because the shell stores a function in the computer's main memory (RAM) instead of in a file on the disk, the shell can access it more quickly than the shell can access a script. The shell also preprocesses (parses) a function so that it starts up more quickly than a script. Finally the shell executes a shell function in the same shell that called it. If you define too many functions, the overhead of starting a subshell (as when you run a script) can become unacceptable.

You can declare a shell function in the ~/.bash_profile startup file, in the script that uses it, or directly from the command line. You can remove functions with the unset builtin. The shell does not keep functions once you log out.

The syntax that declares a shell function is



where the word function is optional, function-name is the name you use to call the function, and commands comprise the list of commands the function executes when you call it. The commands can be anything you would include in a shell script, including calls to other functions.

The first brace ({) can appear on the same line as the function name. Aliases and variables are expanded when a function is read, not when it is executed. You can use the break statement (page 543) within a function to terminate its execution.

Shell functions are useful as a shorthand as well as to define special commands. The following function starts a process named process in the background, with the output normally displayed by process being saved in .process.out:

start_process( ) {
process > .process.out 2>&1 &

The next example shows how to create a simple function that displays the date, a header, and a list of the people who are using the system. This function runs the same commands as the whoson script described on page 265. In this example the function is being entered from the keyboard. The greater-than (>) signs are secondary shell prompts (PS2); do not enter them.

$ function whoson ()
> {
>  date
>  echo "Users Currently Logged On"
>  who
> }

$ whoson
Sun Aug 7 15:44:58 PDT 2005
Users Currently Logged On
zach     console  May 5 22:18
zach     ttyp1    May 5 22:20
zach     ttyp2    May 5 22:21 (bravo.example.co)

Functions in startup files

If you want to have the whoson function always be available without having to enter it each time you log in, put its definition in ~/.bash_profile. Then run .bash_profile, using the . (dot) command to put the changes into effect immediately:

$ cat ~/.bash_profile
stty kill '^u'
whoson ()
    echo "Users Currently Logged On"
$ . ~/.bash_profile

You can specify arguments when you call a function. Within the function these arguments are available as positional parameters (page 564). The following example shows the arg1 function entered from the keyboard.

$ arg1 ( ) {
> echo "$1"
> }
$ arg1 first_arg

See the function switch () on page 261 for another example of a function. "Functions" on page 561 discusses the use of local and global variables within a function.

  • + Share This
  • 🔖 Save To Your Account