function reserved word and omits the parentheses. It is specific to Bash and KornShell (ksh).
function keyword and parentheses. While valid in Bash, it is redundant and generally discouraged in strict style guides.
Compound Commands as Function Bodies
While the grouping command{ ... } is the most common function body, Bash allows a function body to be any compound command. This includes subshells ( ... ), arithmetic evaluations (( ... )), conditional expressions [[ ... ]], and loops (e.g., for, while, case).
Lexical and Syntactic Rules
- Reserved Word Delimiting: The opening curly brace
{is a reserved word, not a shell metacharacter. It must be delimited by whitespace or a shell metacharacter. If the first command inside the block starts with a metacharacter (such as(,<, or>), no space is required after the{. Conversely,(and)are metacharacters, sofunction_name(){is valid without a space before the brace. - Command Termination: When using the
{ ... }grouping command, the list of commands within the body must be explicitly terminated. The final command before the closing brace}must end with a newline, a semicolon (;), or an ampersand (&). Subshells( ... )do not require this explicit termination before the closing parenthesis.
Declaration Mechanics
- Sequential Parsing: Bash is an interpreted language that parses sequentially. A function must be declared lexically prior to its invocation. The shell does not hoist function declarations.
- Symbol Table Overwriting: If a function is declared with a name that already exists in the shell’s function environment, the new declaration overwrites the previous one. Exception: If the existing function has been marked as read-only using
readonly -f, Bash will throw an error (bash: function_name: readonly function) and refuse to overwrite it. - Command Precedence: Once declared, functions take high precedence in the shell’s execution hierarchy. In default Bash, the resolution order is: Aliases > Functions > Built-ins (both special and regular) > External Executables (in
$PATH). Note: Special built-ins only supersede functions when Bash is invoked in strict POSIX mode. - Removal: A declared function remains in the current shell’s memory until the shell terminates or the function is explicitly removed from the symbol table using the
unsetbuilt-in with the-fflag.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More





