Shell function application
1. Definition of Shell function
1 Write the command sequence format together to facilitate repeated use of the command sequence.
2 Function return value:
-
return means to exit the function and return an exit value, which can be displayed by the $? variable in the script.
-
Usage principles:
1. Take the return value as soon as the function ends, because the $? variable only returns the exit status code of the last command executed.
2. The exit status code must be 0~255, and the value will be divided by 256 if it exceeds . -
Note: The returned value 0-255 uses $? carried out.
-
Format 1:
function function name { command sequence }
-
Format 2:
function name () { command sequence }
2. Function parameter transfer
- The parameters of the position variable are passed to the script.
3. The scope of function variables
- Functions in Shell scripts are only valid in the current Shell environment
- Variables in Shell scripts are globally effective by default
- Use the local command to limit the variable to the function
Four. Recursion
- The function calls its own function.
Five. Create a library