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:

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:

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:

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:

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:

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:

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:

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.

In addition to the return value, you can also use the echo command to print a value from a function. For example:

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.