shell function
A named group of commands, defined as name() { ...; }, that you then run like any other command.
In more detail
A function runs in the current shell, so it can change variables and the current directory, and it receives its own $1, $2 and $@. Variables set inside are global unless declared local. Functions replace aliases in scripts and let a long script read as a series of named steps.
Examples
greet() { echo "hello, $1"; }greet adaRelated terms
- local variable — A variable that exists only inside one function call and cannot be seen or changed from outside it.
- return builtin — The builtin that ends a function and sets its exit status, a number from 0 to 255, not a text value.
- positional parameters — The numbered variables $1, $2 and so on that hold the arguments given to a script or function, with $# holding how many there are.
- shell alias — A short name the shell swaps in for the start of a command line, defined with alias name='command'.
Taught in
- Help, History, Completion And Aliases — Bash Scripting & Command Line Automation
- What Bash Does To A Line Before It Runs It — Bash Scripting & Command Line Automation
- Arrays, And Why Dollar At Is Not Dollar Star — Bash Scripting & Command Line Automation
- Functions, local And declare — Bash Scripting & Command Line Automation
- Arguments: Dollar One, Dollar At, shift — Bash Scripting & Command Line Automation
- trap, Cleanup And Temporary Files — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation