Functions
As in programming languages, shell functions are blocks of code that can be accessed by their names.
Function definition
Syntax
You define a new function with the following syntax:
name() {
command1
command2
...
}Example
greetings() {
echo hello
}Storing functions
You can define a new function directly in the terminal. Such a function would however only be available during your current session. Since functions contain code that is intended to be run repeatedly, it makes sense to store function definitions in a file. Before functions become available, the file needs to be sourced (e.g. source file.sh).
A convenient file is ~/.bashrc. The file is automatically sourced every time you start a shell so your functions will always be defined and ready for use.
Here is a video of a previous version of this workshop.