function is a script code block, you can name it yourself, and you can use this function anywhere in the script. If you want this function, just call the name of the function. The advantage of using functions is modularity and code readability.
(1). Function creation syntax
Function creation method 1:
function function name { Order }
Function creation method 2:
function name() { Order }
Note: The function name must be unique in the current script.
How to call the function:
function name parameter 1 parameter 2. .....
You can pass parameters when calling a function. The function uses $1, $2… to refer to the passed parameters.
(2). Examples of the use of functions
Example 1:
[[email protected] ~]# vim a.sh #!/bin/bash function fun1 { echo "hello world" } fun2() { echo $[$1+$2] } fun1 fun2 1 2 [[email Protected] ~]# sh a.sh hello world 3
Example 2: If there is a function with the same name, the last one shall prevail
[[email protected] ~]# vim b.sh #!/bin/bash function fun1 { echo "hello world" } fun1() { echo "This is rewrite" } fun1 [[email Protected] ~]# sh b.sh This is rewrite
(3).return return value
Use the return command to exit the function and return a specific exit code ($?)
[[emailprotected] ~]# vim c.sh #!/bin/bash fun1() { echo "return 3" return 3 echo "why?" } fun1 [[email Protected] ~]# sh c.sh return 3 [[email Protected] ~]# echo $? 3
Note: The return is usually the last line of the function, because once the return command is executed, the commands following the function will not be executed.
The difference between return and exit: Both return and exit can return an exit code, but the difference is that return is an exit function, while exit is to exit the entire script.
(4). Assign function value to variable
As shown in the example below, the function at this time is equivalent to a command, which needs to be called with $() or “.
[[email protected] ~]# vim d.sh #!/bin/bash fun1() { read -p "Input a number:" num echo $[num*5] } num2=`fun1` # can also be $(fun1) echo $num2 [[email Protected] ~]# sh d.sh Input a number: 2 10
(5). Passing external parameters into the function
As mentioned earlier, the calling function can be followed by parameters, and the function can be called in the form of $n .
[[email protected] ~]# vim e.sh #!/bin/bash fun1() { echo $1 } fun2() { echo $1 } fun3() { echo $1 } fun1 $1 fun2 /root/a.sh fun3 5 [[email Protected] ~]# sh e.sh hello hello /root/a.sh 5
(6). Function parameters
In a script, the parameters outside the function, the function can be called directly, and the parameters inside the function, as long as the function has been run , The external can also be called directly.
[[email protected] ~]# vim f.sh #!/bin/bash num=5 fun1() { num2=$[num*10] } fun1 echo $num2 [[email Protected] ~]# sh f.sh 50
Note: The parameters in the script can only be used within the script
function function Name { Order }
function name() { Order }
function name parameter 1 parameter 2 ......
[[email protected] ~]# vim a.sh #!/bin/bash function fun1 { echo "hello world" } fun2() { echo $[$1+$2] } fun1 fun2 1 2 [[email Protected] ~]# sh a.sh hello world 3
[[email protected] ~]# vim b.sh #!/bin/bash function fun1 { echo "hello world" } fun1() { echo "This is rewrite" } fun1 [[email Protected] ~]# sh b.sh This is rewrite
[[email protected] ~]# vim c.sh #!/bin/bash fun1() { echo "return 3" return 3 echo "why?" } fun1 [[email Protected] ~]# sh c.sh return 3 [[email Protected] ~]# echo $? 3
[[email protected] ~]# vim d.sh #!/bin/bash fun1() { read -p "Input a number:" num echo $[num*5] } num2=`fun1` # can also be $(fun1) echo $num2 [[email Protected] ~]# sh d.sh Input a number: 2 10
[[email protected] ~]# vim e.sh #!/bin/bash fun1() { echo $1 } fun2() { echo $1 } fun3() { echo $1 } fun1 $1 fun2 /root/a.sh fun3 5 [[email Protected] ~]# sh e.sh hello hello /root/a.sh 5
[[email protected] ~]# vim f.sh #!/bin/bash num=5 fun1() { num2=$[num*10] } fun1 echo $num2 [[email Protected] ~]# sh f.sh 50