A function is the name of a command or a set of commands.
Functions can make the program modular, thus improving efficiency.
The shell stores functions in memory rather than in disk files, so accessing the function is much faster than accessing the script.
The shell pre-process the function so that it starts faster.
Before you can use a function, you must first create the function in the program.
Creating a function is also called defining a declaration of a function or function.
[function] Function name {command table [return]}
which
(1) The keyword function means defining a function, which can be omitted, followed by the function name. (You can add a pair of empty parentheses after the function name)
(2) Return in the function returns the exit state value of the last command in the function or the given parameter value.
(3) If you use the Exit command in a function, you can exit the entire script. If the function exits, it returns to the place where the function was called in the script.
(4) The break statement can be used to interrupt the execution of the function.
(5) Use internal command declare-f to display a defined list of functions. If only the name of the function is displayed, you can use the command declare-f.
(6) You can use the command export-f to output a function to the shell
(7) may use command unset-f to remove functions from shell memory
function Definition Location : Can be placed in the ~/.bash_profile file, or in a script that uses the function, or can be placed directly on the command line.
Function deletion: You can delete the deletion using the unset command, and the shell will no longer hold these functions once the user logs off.
function invocation , involving function execution, function parameter passing, function return value, load function, and function deletion. Before a function is executed, it must be defined first.
Execute function
When the shell executes an alias, function, internal command, or a disk-based executable, it always executes the alias, then the function, the internal command, and finally the executing program.
Execute functions, simply enter the function name directly.
Example
1 #! /bin/bash 2 #filename a.sh 3 function Show () 4 {5 Date 6 echo "The LogName:" 7 echo $LOGNAME 8} 9 Show
Functions created from the command line are automatically deleted when the user exits the login.
function parameter passing
Function parameters can be passed by positional variables.
In the command line that invokes the function, the position of the parameter is defined as follows
$ function Name parameter 1 parameter 2 parameter 3 ...
1 #! /bin/bash 2 #filename a.sh 3 function Show () 4 {5 echo $a $b $b $c $d 6 echo $ $4 7} 8 a=111 9 b=222 Ten c=333 d=444 echo "function Begin" show a b c D echo "function End"
# bash A.shfunction Begin111 222 222 333 444a b C dfunction End
1 #! /bin/bash 2 #filename a.sh 3 cube () 4 {5 item= ' expr $ \* $ \* $ ' 6} 7 8 echo "Please enter a integer:" 9 Read N i=1 result=0 item= ($i-le $N) do $i result= ' expr $RESULT + $ITEM ' i=$ ( $i + 1))-Done echo "compute 1 ... $N de Li Fang He is: $RESULT" NOTE: Do not add spaces at will, after item, = Before and after
return value of the function
The keyword return in a function can be placed anywhere in the body of the function, and is typically used to return certain values.
After the program executes to return, the function stops executing and returns to the calling line of the main program.
The return value, an integer between 0~256. The return value is saved to the variable $?.
If no return value is specified, the returned function value is the exit state value of the last command executed in the function.
Load function
The definition of a function can be placed in a ~/.bash_profile file, either directly on the command line or in a script file.
If you need to use a function that is saved in another script file, you can load them into memory by using the source command or the. command for use by the current script.
Local temp defines a native variable
As shown below, it is divided into two files a.sh and a1.sh. In a1.sh, use source a.sh to invoke a.sh.
1 #! /bin/bash 2 #filename:a.sh 3 4 function square 5 { 6 local temp 7 let temp=$1*$1 8 echo "$1 square : $temp" 9 } 10 11 function cube 12 { 13 local temp 14 let temp=$1*$1*$1 15 echo "$1 cube : $temp " 16 } 1 #! /bin/bash 2 #filename: a1.sh 3 source a.sh 4 echo "Please enter an integer:" 5 read N 6 i=1 7 while [ $i -le $N ] 8 do 9 square $i 10 i=$ (($i + 1)) 11 done 12 echo "------------------" 13 i=1 14 while [ $i -le $N ] 15 do 16 cube $i 17 i=$ (($i + 1)) 18 done# bash a1.shplease enter an integer:31 square : 12 square : 43 square : 9------------------1 cube : 12 cube : 83 cube : 27#
Delete a function
unset-f command to remove functions from shell Memory: unset-f function name
DECLARE-F display of defined functions in memory
Scope, which is the range of variables. Scope that defines the visibility and life cycle of the variable.