Shell Learning 22 - Shell Function Arguments

In the shell, you can pass arguments to a function when you call it. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 represents the first parameter, $2 represents the second parameter...
Example of a function with parameters:
#!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # The number of parameters
echo "The string of the parameters is $* !" # all parameters passed to the function
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
Run the script:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
Note that $10 cannot get the tenth parameter, ${10} is required to get the tenth parameter. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special variables used to handle parameters, as mentioned earlier:
Special variable Description
$# The number of arguments passed to the function.
$* displays all arguments passed to the function.
$@ is the same as $*, but slightly different, see Shell special variables.
The return value of the $? function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684425&siteId=291194637