Definition
A shell function is a named block of shell commands that runs in the current shell process when its name is invoked like a command. Functions take positional arguments through the same dollar-one, dollar-two convention used by scripts, return an integer exit status, and can declare local variables that vanish when the function returns. They are the basic unit of decomposition inside a non-trivial shell script.
A function is defined with either the keyword-name-brace form or the name-parentheses-brace form. Both create an entry in the shell function table that the parser consults before searching the path for an external command. This means a function can shadow a system program of the same name, which is occasionally useful and occasionally a footgun.
Why it matters
How it works
When the parser encounters a function definition, it stores the body in memory rather than executing it. A later invocation pushes a new positional-parameter frame, runs the body in the same shell process, and pops the frame on return. Because functions run inside the current shell — not a subshell — they can modify shell options, change the working directory, or set variables visible to the caller. The local keyword scopes a variable to the function and any of its children, which is the recommended default for any variable a function does not deliberately export.
Return values come in two flavours. The integer exit status, set by the return keyword or by the last command executed, communicates success or failure to the caller. The function's standard output, captured via command substitution, communicates a string or computed result. Combining the two — exit status for control flow, stdout for data — is the idiomatic shell pattern for building modular scripts that grow without becoming unreadable.