Bash Functions
Defining and using functions
Function Definition
function_name() {
commands
}
# Or with function keyword:
function function_name {
commands
}
Call Function
function_name # call function
function_name arg1 arg2 # call with arguments
Function Arguments
$1, $2, ... # positional parameters
$@ # all arguments
$# # number of arguments
# Example:
greet() {
echo Hello $1
}
greet John
Return Values
return 0 # return success
return 1 # return error
echo value # return string via stdout
# Capture return:
result=$(function_name)
Local Variables
local var=value # local to function
# Example:
my_func() {
local name=John
echo $name
}