Bash functions are a handy way to group a series of commands that you often use together. They allow you to reuse code, make your scripts more organized and easier to read, and save you time by not having to type out the same commands over and over again.
To create a function in Bash, you use the function keyword followed by the name of the function and a pair of curly braces that enclose the commands that make up the function. For example:
1 2 3 | function greeting { echo "Hello, world!" } |
This creates a function called `greeting` that simply outputs the string “Hello, world!” when it is called.
To call a function, you simply type its name in your shell script. For example:
1 | greeting |
This will execute the commands within the `greeting` function, which in this case is just the echo command.
Bash Function with Arguments
You can also pass arguments to a function by listing them within the parentheses when you call the function. For example:
1 2 3 4 5 | function greeting { echo "Hello, $1!" } greeting "John Doe" |
This will output “Hello, John Doe!” because the `$1` variable within the function refers to the first argument passed to the function (in this case, “John Doe”). You can use $2, $3, and so on to refer to the second, third, and subsequent arguments.
You can also specify default values for arguments if they are not provided when the function is called. For example:
1 2 3 4 5 6 7 | function greeting { name=${1:-World} echo "Hello, $name!" } greeting greeting "John" |
The first call to `greeting` will output “Hello, World!” because the name variable is set to the default value of “World” if no argument is provided. The second call to `greeting` will output “Hello, John!” because the name variable is set to the value of the first argument (“John”).
Define Argument Type Using Builting Commands
Using the `declare` builtin command, you can also specify the types of arguments that a function expects. For example:
1 2 3 4 5 6 7 | function add { declare -i num1=$1 declare -i num2=$2 echo $((num1 + num2)) } add 1 2 |
This function expects two integer arguments and adds them together using the `$((…))` arithmetic expansion syntax. The `declare -i` flag tells Bash to treat the variables as integers.
Bash Functions with Local Variables
Bash functions also have local variables, which are variables that are only visible within the function and are not accessible outside of it. To create a local variable, you use the `local` keyword followed by the name of the variable. For example:
1 2 3 4 5 6 7 8 9 | function count { local i for i in {1..5}; do echo $i done } count echo $i |
The count function creates a local variable called `i` that is used to count from 1 to 5. The echo `$i` statement outside of the function will not output anything because the `i` variable is not defined outside of the function.
Function with Return Values
You can also return a value from a function using the `return` builtin command. The value of the return command is the exit status of the function, which is a number that indicates whether the function was completed successfully or encountered an error. A `return` value of `0` indicates success, while a non-zero value indicates an error.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function add { if [[ $# -ne 2 ]]; then return 1 fi declare -i num1=$1 declare -i num2=$2 return $((num1 + num2)) } add 1 2 echo $? add 1 echo $? |
The add function checks to make sure that it has received exactly two arguments. If it has not, it returns a value of 1 to indicate an error. Otherwise, it adds the two arguments together and returns the result.
The `echo $?` command after each call to the add function will output the return value of the function. The first call will output 3, while the second call will output 1.
Print Values In Function
In addition to the return value, you can also use the echo command to print a value from a function. For example:
1 2 3 4 5 6 7 8 9 10 11 | function add { if [[ $# -ne 2 ]]; then return 1 fi declare -i num1=$1 declare -i num2=$2 echo $((num1 + num2)) } result=$(add 10 20) echo "The result is $result." |
This code will output “The result is 30.” because the add function is called using command substitution (the $(…) syntax) and the result is stored in the result variable.
Bash functions can be very powerful and are a valuable tool for organizing and simplifying your scripts. They allow you to reuse code, make your scripts more readable, and save you time by not having to type out the same commands over and over again.