I. define functions
CopyCodeThe Code is as follows: function fname ()
{
Statements;
}
OrCopy codeThe Code is as follows: fname ()
{
Statements;
}
Ii. Call Functions
You can call a function by using the function name:
$ Fname; # execute a function
Parameters can be passed to the function and accessed by the script:
Fname arg1 arg2; # Passing Parameters
The following is the definition of the function fname. The function fname contains methods for accessing function parameters.
Copy codeThe Code is as follows: fname ()
{
Echo $1, $2; # access parameter 1 and parameter 2
Echo "$ @"; # print all parameters in a list at a time
Echo "$ *"; # similar to $ @, but the parameter is used as a single entity (string)
Return 0; # Return Value
}
Similarly, parameters can be passed to the script and accessed through Script: $0 (Script Name.
Annotations on function parameters in Bash:
$1 is the first parameter.
$2 is the second parameter.
$ N is the nth parameter.
"$ @" Is extended to "$1" "$2" "$3" and so on.
"$ *" Is extended to "$ 1C $ 2C $ 3C", where C is the first character of IFS.
"$ @" Is used most. Because "$ *" treats all parameters as a single string, it is rarely used.