One, the definition function
Copy Code code as follows:
function fname ()
{
statements;
}
Or
Copy Code code as follows:
Second, call function
You can call a function by using only the function name:
$ fname; #执行函数
You can pass parameters to a function and be accessed by the script:
fname arg1 arg2; # Pass Parameters
The following is the definition of function fname. In function fname, a variety of methods of accessing function parameters are included.
Copy Code code as follows:
FName ()
{
echo $, $; # Access parameter 1 and parameter 2
echo "$@"; # Print all parameters at once in list mode
echo "$*"; # is similar to $@, but parameters are treated as a single entity (string)
return 0; # return value
}
Similarly, parameters can be passed to the script and accessed via script:$0 (script name).
Note about the function arguments in bash:
$ is the first parameter.
$ is the second parameter.
$n is the nth argument.
"$@" is extended to "$" "$" "$" and so on.
"$*" is extended to "$1c$2c$3c", where C is the first character of the IFS.
"$@" is used most. Because "$*" treats all parameters as a single string, it is rarely used.