Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Shell Functions

Shell Functions

In linux shell you can define your own functions and then call them freely in shell scripts.

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

[

function

]

funname

[

(

)

]

{

    action;

   

[

return

int;

]

}

Explanation:

The following example defines a function and calls it:

Example

#!/bin/bash

author:Runoob Tutorial

url:www.runoob.com

demoFun

(

)

{

   

echo

"This is my first shell function!"

}

echo

"-----function starts executing-----"

demoFun

echo

"-----function execution complete-----"

Output result:


-----Function execution started-----
这是我的第一个 shell 函数!
-----Function execution finished-----

The following defines a function with a return statement:

Example

#!/bin/bash

author:Runoob Tutorial

url:www.runoob.com

funWithReturn

(

)

{

   

echo

"This function adds the two input numbers..."

   

echo

"Enter the first number: "

   

read

aNum

   

echo

"Enter the second number: "

   

read

anotherNum

   

echo

"The two numbers are $aNum and $anotherNum !"

   

return

$

(

(

$aNum

$anotherNum

)

)

}

funWithReturn

echo

"The sum of the two input numbers is $? !"

The output is similar to the following:


这个函数会对输入的两个数字进行相加运算...
Enter the first number:
1
Enter the second number:
2
两个数字分别为 1 和 2 !
输入的两个数字之和为 3 !

The function's return value is obtained through $? after calling the function.

**Note:** All functions must be defined before use. This means you must place functions at the beginning of the script; they can only be used once the shell interpreter first encounters them. To call a function, simply use its function name.

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

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

Example

funWithReturn

(

)

{

   

echo

"This function adds 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 $aNum and $anotherNum !"

   

echo

$sum

 

Output the sum of the two numbers

}


Function parameters

In Shell, you can pass parameters when calling a function. Inside the function body, parameter values are obtained through the form $n, for example, $1 is the first parameter, $2 is the second parameter...

Example of a function with parameters:

Example

#!/bin/bash

author:Runoob Tutorial

url:www.runoob.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 single 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 for handling parameters:

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