The shell function is similar to a shell script, which holds a series of instructions, but the Shell function is in memory, not the hard disk file, so it is fast, and the shell can preprocess the function, so the function starts faster than the script.
1. Function definition
1234 |
function 函数名() { 语句 [ return ] } |
The keyword function representation defines a function, which can be omitted, followed by the function name, sometimes the function name can be followed by a parenthesis, the symbol "{" represents the entry of the function Execution command, the symbol can also be in the function name line, "}" means the end of the function body, between the two curly braces is the function body.
The statement part can be any shell command, or other functions can be called.
If you use the Exit command in a function, you can exit the entire script, which normally returns the part of the calling function to continue after the function ends.
You can use the break statement to interrupt the execution of a function.
Declare–f can display a list of defined functions
Declare–f can display only defined function names
Unset–f can remove functions from shell memory
Export–f output the function to the shell
In addition, the definition of a function can be placed in a. bash_profile file or in a script that uses a function, can also be placed directly on the command line, and the function can be deleted using the internal unset command. Once the user logs off, the shell will no longer hold these functions.
2. Invocation of function
Example of a function call:
12345678 |
#!/bin/bash function show() { echo "hello , you are calling the function" } echo "first time call the function" show echo "second time call the function" show |
3. Transfer of function parameters
Functions can pass parameters through positional variables. For example
Function Name parameter 1 parameter 2 parameter 3 parameter 4
When the function executes, it corresponds to parameter 1, and so on.
Instance:
12345678 |
#!/bin/bash function show() { echo "hello , you are calling the function $1" } echo "first time call the function" show first echo "second time call the function" show second |
4. return value of function
The keyword "return" in the function can be placed anywhere in the body of the function, usually to return certain values, the shell will stop execution after execution to return, return to the main program's call line, return value can only be an integer between 0~256, the return value will be saved to the variable "$?" In
Instance:
1234567891011121314151617181920 |
#!/bin/bash
function
abc() {
RESULT=`
expr
$1 \% 2`
#表示取余数
if
[ $RESULT –
ne
0 ] ;
then
return
0
else
return
1
fi
}
echo "Please enter a number who can devide by 2"
read
N
abc $N
case
$?
in
0)
echo
"
yes
,it is”
;;
1)
echo
“no ,it isn’t”
;;
esac
|
Here to pay attention to the parameters passed, the above read in the number, must be added to the $ symbol to pass to the function, I just started not know where is wrong, find a half-day to know is here error.
5. Loading of functions
If the function is in another file, how do we call it?
Here's a way. For example, the show function is written in function.sh, and we can use the source command
This makes it possible to invoke the.
6. Deletion of functions
Usage: unset–f function name
7. Variable Scope of function
By default, a variable has a global scope, and if you want to set it as a local scope, you can add the local
For example:
Using local variables allows the function to automatically release memory space occupied by variables after execution, thus reducing the consumption of system resources, and it is particularly important to define and use local variables when running large programs.
8. Nesting of functions
Functions can be nested, examples:
123456789101112131415 |
#!/bin/bash
function
first() {
function
second() {
function
third() {
echo
"------this is third"
}
echo
"this is the second"
third
}
echo
"this is the first"
second
}
echo
"start..."
first
|
Linux shell functions