How do bash functions work?
Answer
Bash functions group reusable commands: function greet() { echo "Hello, $1"; } or equivalently greet() { echo "Hello, $1"; }. Call with greet Alice. Inside a function, $1, $2, etc. refer to the function's arguments (not the script's). local var=value declares a variable local to the function, preventing it from leaking into the global scope — always use local for function variables. Functions return exit codes (0–255) via return N; use echo and command substitution to return string values: result=$(my_func). Functions must be defined before they are called in the script. Source a functions library into multiple scripts with source functions.sh.
Previous
How do you write if/for/while/case statements in bash?
Next
What is $? and how does exit status error handling work in bash?