Shell Functions

Linux shell allows users to define functions, which can then be called freely in shell scripts.

The format for defining a function in shell is as follows:

[ function ] funname [()]

{

    action;

    [return int;]

}

Note:

  • 1. You can define withfunction fun()definition, or directlyfun()define without any parameters.
  • 2. For the return value, you can explicitly add:returnreturn. If not added, the result of the last command will be used as the return value.returnfollowed by a numeric valuen(0-255).

The following example defines a function and calls it:

Example

#!/bin/bash
# author: Example Tutorial
# url:www.example.com

demoFun(){
    echo "This is my first shell function!"
}
echo "-----The function starts executing-----"
demoFun
echo "-----The function execution is complete-----"

Output result:

-----函数开始执行-----
这是我的第一个 shell 函数!
-----函数执行完毕-----

The following defines a function with areturnstatement:

Example

#!/bin/bash
# author: Example Tutorial
# url:www.example.com

funWithReturn(){
    echo "This function will add the two input numbers..."
    echo "Enter the first number: "
    read aNum
    echo "Enter the second number: "
    read anotherNum
    echo The two numbers are$aNumand$anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of the two input numbers is $? !"

The output is similar to the following:

这个函数会对输入的两个数字进行相加运算...
输入第一个数字: 
1
输入第二个数字: 
2
两个数字分别为 1 和 2 !
输入的两个数字之和为 3 !

The function return value, after calling the function, is obtained through$? to obtain it.

Note:All functions must be defined before use. This means the function must be placed at the beginning of the script, and it can only be used when the shell interpreter first encounters it. To call a function, just use its function name.

Note: returnThe return statement can only return an integer between 0 and 255, and the sum of the two input numbers may exceed this range.

To solve this problem, you can modify the return statement and directly use echo to output the sum instead of using return:

Example

funWithReturn(){
    echo "This function will add the two input numbers..."
    echo "Enter the first number: "
    read aNum
    echo "Enter the second number: "
    read anotherNum
    sum=$(($aNum + $anotherNum))
    echo The two numbers are$aNumand$anotherNum !"
    echo $sum  # Output the sum of the two numbers
}

Function Parameters

In Shell, when calling a function, you can pass parameters to it. Inside the function body, the parameter values are obtained in the form of $n. For example, $1 represents the first parameter, $2 represents the second parameter...

Example of a function with parameters:

Example

#!/bin/bash
# author: Example Tutorial
# url:www.example.com

funWithParam(){
    echo "The first parameter is $1 !"
    echo "The second parameter is $2 !"
    echo "The tenth parameter is $10 !"
    echo The tenth parameter is${10} !"
    echo The eleventh parameter is${11} !"
    echo "The total number of parameters is $# !"
    echo "Output all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

Output result:

第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

Note that $10 cannot get the tenth parameter; to get the tenth parameter, you need ${10}. When n>=10, you need to use ${n} to get the parameter.

In addition, there are several special characters used to handle parameters:

Parameter handling Description
$# The number of parameters passed to the script or function
$* Display all parameters passed to the script as a single string
$$ The current process ID number of the running script
$! The ID number of the last process running in the background
$@ Same as $*, but when used, add quotes and return each parameter within quotes.
$- Display the current options used by Shell, with the same function as the set command.
$? Display the exit status of the last command. 0 means no error; any other value indicates an error.
Other extensions