Bash Functions
About bash functions
- Also called Shell Functions
- Once a function is defined, it is used like a "regular" command
- Shell functions are executed in the current shell context; no new process is created to interpret them.
Function Syntax
- Functions are declared using this syntax:
1fname () compound-command [ redirections ]
2
3or
4
5function fname [()] compound-command [ redirections ]
- Examples:
1hello () {
2 printf "1: %s\n" $1
3 printf "2: %s\n" $2
4}
5
6function hello2 {
7 printf "This is hello2"
8}
- Create those functions by using source command
- Call your functions as if it was a "regular" command:
- If you omit the parentheses and you don't use the function keyword, bash cannot understand you are creating a new function.
So this is not good:
- You cannot specify parameters inside the function parentheses.
- Delete your function by using unset command