English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Shell Functions

Linux shell can define user-defined functions, and then they can be called freely in shell scripts.

The format for defining functions in shell is as follows:

[function] funname [()]
{
    action;
    [return int;]
}

Description:

  • 1Can be defined with function fun() Define, or directly fun() Define, without any parameters.

  • 2Parameters can be returned with return, which can show addition: return returns, if not added, it will take the result of the last command as the return value. After return, follow the number n (0-255

The following example defines a function and calls it:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
demoFun()
    echo "This is my first shell function!"
}
echo ""-----Function execution starts-----"
demoFun
echo ""-----Function execution completed-----"

Output Result:

-----Function execution starts-----
This is my first shell function!
-----Function execution completed-----

Below is a function defined with a return statement:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
funWithReturn()
    echo "This function will perform addition operations on 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:

This function will perform addition operations on the two input numbers...
Enter the first number: 
1
Enter the second number: 
2
The two numbers are 1 and 2 !
The sum of the two input numbers is 3 !

The return value of the function can be obtained after calling it through $?.

Note: All functions must be defined before use. This means that the function must be placed in the beginning part of the script, until the shell interpreter first detects it, and then it can be used. The function is called only by its function name.

Function parameters

In Shell, when calling a function, you can pass parameters to it. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1Indicates the first parameter, $2Indicates the second parameter...

Example of Function with Parameters:

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.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 "As a string, output all parameters $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

Output Result:

The first parameter is 1 !
The second parameter is 2 !
The tenth parameter is 10 !
The tenth parameter is 34 !
The eleventh parameter is 73 !
The total number of parameters is 11 !
Outputs all parameters as a string 1 2 3 4 5 6 7 8 9 34 73 !

Note that $10 The tenth parameter cannot be obtained. To get the tenth parameter, you need to use ${10}. When n>=10when, 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
$* Displays all parameters passed to the script as a single string
$$ The current process ID number of the script running
$! The process ID number of the last process run in the background
$@ With $*Same, but add quotes when using it, and return each parameter within the quotes.
$- Displays the current options used by Shell, similar to the function of the set command.
$? Displays the exit status of the last command executed. 0 indicates no error, any other value indicates an error.