13.16. Functions

 <  Day Day Up  >  

Bash functions are used to execute a group of commands with a name within the context of the current shell (a child process is not forked). They are like scripts, only more efficient. Once defined, functions become part of the shell's memory so that when the function is called, the shell does not have to read it in from the disk as it does with a file. Often functions are used to improve the modularity of a script. Once defined, functions can be used again and again. Although functions can be defined at the prompt when running interactively, they are often defined in the user 's initialization file, .bash_profile . They must be defined before they are invoked.

13.16.1 Defining Functions

There are two ways to declare a bash function. One way, the old Bourne shell way, is to give the function name followed by a set of empty parentheses, followed by the function definition. The new way (Korn shell way) is to use the function keyword followed by the function name and then the function definition. If using the new way, the parentheses are optional. The function definition is enclosed in curly braces. It consists of commands separated by semicolons. The last command is terminated with a semicolon. Spaces around the curly braces are required. Any arguments passed to the function are treated as positional parameters within the function. The positional parameters in a function are local to the function. The local built-in function allows local variables to be created within the function definition. Functions may also be recursive; that is, they can call themselves an unlimited number of times.

FORMAT

 function_name () { commands ; commands; } function function_name { commands ; commands; } function function_name () { commands ; commands; } 

Example 13.81.
 1   $  function greet { echo "Hello $LOGNAME, today is $(date)"; }  2   $  greet   Hello ellie, today is Wed Jul 14 14:56:31 PDT  2004  3   $  greet () { echo "Hello $LOGNAME, today is $(date)"; }  4   $  greet   Hello ellie, today is Wed Jul 14 15:16:22 PDT  2004  5   $  declare -f   declare -f greet()   {   echo "Hello $LOGNAME, today is $(date)"   }  6   $  declare -F   [a]   declare -f greet  7   $  export -f greet  8   $  bash   # Start subshell  9   $  greet   Hello ellie, today is Wed Jul 14 17:59:24 PDT  2004  

EXPLANATION

  1. The keyword function is followed by the name of the function, greet . The function definition is surrounded by curly braces. There must be a space after the opening curly brace . Statements on the same line are terminated with a semicolon.

  2. When the greet function is invoked, the command(s) enclosed within the curly braces are executed in the context of the current shell.

  3. The greet function is defined again using the Bourne shell syntax, the name of the function, followed by an empty set of parentheses, and the function definition.

  4. The greet function is invoked again.

  5. The declare command with the “f switch lists all functions defined in this shell and their definitions.

  6. The declare command with the “F switch lists only function names .

  7. The export command with the “f switch makes the function global (i.e., available to subshells).

  8. A new bash shell is started.

  9. The function is defined for this child shell because it was exported.

Example 13.82.
 1   $  function fun {  echo "The current working directory is $PWD."          echo "Here is a list of your files: "          ls          echo "Today is $(date +%A).";  }  2   $  fun   The current working directory is /home.   Here is a list of your files:   abc      abc123   file1.bak   none       nothing   tmp   abc1     abc2     file2       nonsense   nowhere   touch   abc122   file1    file2.bak   noone      one   Today is Wednesday.  3   $  function welcome { echo "Hi  and "; }  4   $  welcome tom joe   Hi tom and joe  5   $  set jane anna lizzy  6   $  echo $*   jane anna lizzy  7   $  welcome johan joe   hi johan and joe  8   $  echo   johan joe  9  $  unset -f welcome   # unsets the function  

EXPLANATION

  1. The function fun is named and defined. The keyword function is followed by the function's name and a list of commands enclosed in curly braces. Commands are listed on separate lines; if they are listed on the same line, they must be separated by semicolons. A space is required after the first curly brace or you will get a syntax error. A function must be defined before it can be used.

  2. The function behaves just like a script when invoked. Each of the commands in the function definition is executed in turn .

  3. There are two positional parameters used in the function welcome . When arguments are given to the function, the positional parameters are assigned those values.

  4. The arguments to the function, tom and joe , are assigned to $1 and $2 , respectively. The positional parameters in a function are private to the function and will not interfere with any used outside the function.

  5. The positional parameters are set at the command line. These variables have nothing to do with the ones set in the function.

  6. $* displays the values of the currently set positional parameters.

  7. The function welcome is called. The values assigned to the positional parameters are johan and joe .

  8. The positional variables assigned at the command line are unaffected by those set in the function.

  9. The unset built-in command with the “f switch unsets the function. It is no longer defined.

13.16.2 Listing and Unsetting Functions

To list functions and their definitions, use the declare command. In bash versions 2.x and above, declare “F lists just function names. The function and its definition will appear in the output, along with the exported and local variables. Functions and their definitions are unset with the unset “f command.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net