How do you define and use functions in bash?

Answer

Bash functions are defined as: function_name() { commands; } or function function_name { commands; }. Call with just: function_name arg1 arg2. Inside, arguments are accessed as $1, $2, etc. (separate from script-level positional params). Functions share the shell's variable scope by default. Use local varname to create local variables that don't leak out. Functions return exit codes via return N (not values — for values, use echo and capture with $()). Best practice: define all functions before calling them. Functions enable code reuse, testing, and cleaner scripts. Example: log() { echo "[$(date)] $*" >> app.log; }