< Day Day Up > |
13.16. FunctionsBash 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 FunctionsThere 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
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 $1 and $2"; } 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 $1 $2 johan joe 9 $ unset -f welcome # unsets the function EXPLANATION
13.16.2 Listing and Unsetting FunctionsTo 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 > |