Function
Creating a Function
or
Function Return Value
There are multiple forms of return values.
Using $?
Represents the exit status code of the last command in the function.
Using Return
Essentially, it’s the exit status code, ranging from [0, 255], and exceeding will be modulo 256.
Using Standard Output
result
will save all the standard output from the function.
Function Parameters
The $#
, $1
, and parameters within the function are independent of the outer parameters.
Variable Scope
Outside the Function
Accessible anywhere.
Inside the Function
Using Local
local
variables only take effect within the function and are independent of external variables with the same name.
Is a Variable Defined
Can use parameter expansion’s + marker.
Is a Function Defined
Later-defined functions with the same name will override previously defined functions with the same name, so it’s a good idea to check before defining a function.
For example, writing the function in the .bashrc file.
The function is defined, exit code is 0, not defined, exit code is 1.