In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...
Examples of functions with parameters:
#!/bin/Bashfunwithparam () {Echo "The value of the first paramter is $1!" Echo "The value of the second paramter is $2!" Echo "The value of the tenth paramter is $10!" Echo "The value of the tenth paramter is ${10}!" Echo "The value of the eleventh paramter is ${11}!" Echo "The amount of the parameters is $#!"#参数个数Echo "The string of the parameter is $*"#传递给函数的所有 >parameter}funwithparam1 2 3 4 5 6 7 8 9 the the
To run the script:
The value of the first paramter is1!The value of the second paramter is2!The value of the tenth paramter isTen!The value of the tenth paramter is the!The value of the eleventh Paramter is the!The amount of the parameters is One! thestringof the parameter is1 2 3 4 5 6 7 8 9 the the
Note that the $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special variables to handle the parameters, as mentioned earlier:
Special Variables |
Description |
$# |
The number of arguments passed to the function. |
$* |
Displays all arguments passed to the function. |
[Email protected] |
Same as $*, but slightly different. |
$? |
The return value of the function. |
Shell function parameters