| by Arround The Web | No comments

Create Bash Functions with Arguments

To create efficient code, bash functions are used in shell scripts. In essence, these are a set of instructions that can be used again throughout the program. Additionally, it enables programmers to divide long, complex scripts into manageable chunks that may be invoked as needed. In this article, we will go through how to pass an argument to a bash function to build a function with an argument. The parameter may be a string, an integer, or something else entirely. Different methods of sending arguments to bash functions will be used in this article.

Passing Argument to the Bash Function

In this section, we will show you how to provide an argument for a bash function in Linux. But first, we will demonstrate how to create a function in bash. The use of functions in bash scripting is a fantastic way to reuse content. A group of commands that can be called repeatedly within a bash script might be referred to as a bash module. Functions in bash are there to make your programs easier to read and prevent you from entering the same script repeatedly.

For writing the program, we will use the bash shell, which is “#!/bin/bash.” Then, in the following line, we will define an essentially user-defined function, so we will give it the name of our choice. Here, we will name the function “func,” and we will use the round brackets “()” that we know are used with functions. We will then use curly brackets inside of these brackets. We use the “echo” command to print the statement that contains the text “I love my country” and because this statement is embedded in the function, it will be displayed in the output when the function is called. The function will now be called at the end of the script. Simply enter the method name “func” without any parentheses to accomplish this.

#!/bin/bash
func () {
         echo “I love my country”
}
func

The command to display the output of a bash script in the terminal will now be entered into the terminal. To do this, we will first enter the keyword “bash” followed by a space, then the name of the bash script, “filebash.sh”.

Linux@linux:~/Desktop$ bash filebash.sh

The string that we used inside the function “func” and wanted to display on the screen after calling the function will be displayed when we run this command. As you can see, the sentence “I love my country” is displayed in the image below.

Now, in the following section, we will give an argument for the bash function. To do this, we’ll construct a script in bash in which we first add the shell, as we did earlier, then define a function with the name “func1(),” followed by curly brackets. The body of the function is located within the curly brackets. Therefore, inside the body of the function, we use the “echo” command to print the function name “func1” and pass the bash parameter “$1”. After exiting the function’s body, we call it in the following line and pass the string “Goodluck” as an argument. This string is kept in the variable “$1”.

#!/bin/bash
func1 () {
         echo “func1 $1
}
func1 “Goodluck”

The script’s output is then displayed on the terminal by typing the same command that is also utilized in the previous step.

Linux@linux:~/Desktop$ bash filebash.sh

As you can see in the image below, when we ran this command, it displayed a statement that included the function name “func1” as well as the string “Goodluck” that we had supplied as an argument to the bash function. It will be saved in the parameter “$1”.

Passing Multiple Arguments to the Bash Function

While this section is similar to the one before it, we will pass multiple parameters here rather than a single argument in the earlier section. For this, we are going to develop a script in which we first add the bash shell, which is “#!/bin/bash,”. Then, define a function with the name “user_function(),” followed by the body of the function, which is created using curly brackets. In between these brackets, we follow the identical steps as in the previous section, inputting the statement “user_function” which is the name of the function and the parameters “$1”, “$2”, “$3”, and “$4” with the “echo” command.

Then, in the following step, we will call the function with the arguments by using the name of the function, “user_function,” as well as the four strings that make up the argument: “I”, “love”, “my” and “country”. The strings must be written with inverted commas. The parameters for bash will be replaced with these strings that we supply as arguments. The first string will be stored in “$1,” the second in “$2,” the third in “$3,” and the fourth in “$4”.

#!/bin/bash
user_function () {
         echo “user_function $1 $2 $3 $4
}
user_function “I” “love” “my” “country”

Now, we are going to use the command to open the bash script on the terminal by first typing “bash” and then the script’s name, “filebash.sh”.

Linux@linux:~/Desktop$ bash filebash.sh

You can see in the image below that when the command is successfully executed, it displays the function name, “user function,” as well as the string “I love my country” which we had supplied as an argument in the bash function.

Using the Division Method in the Bash Function with an Integer Argument

In this section, we will divide an integer using a bash script and send the result as input to the function. To accomplish this, we will write a script in which we first add the bash shell as we did in the previous step. Then, define the function “user function()” then use curly brackets inside of which we define the variable “div” and store the division operation using the dollar sign “$.” We then use double round brackets inside of which we use two parameters, “$1” and “$2,” and between these two parameters we insert the division operator to perform division.

Then, we use the “echo” command in the following line, passing the input statement “dividing 60 by 10 is” as well as the argument “div” with the value “$” because the answer of the division is stored in it. The values we use in the division, “60” and “10,” will be passed as arguments to the function when we call it using the function “user_define.”

#!/bin/bash
user_function () {
div=$( ( $1 / $2 ) )
 echo “dividing 60 by 10 is : $div
}
user_function 60 10

The following command should be used to display the outcome on the screen as the subsequent step.

Linux@linux:~/Desktop$ bash filebash.sh

You can see the division’s outcome shown when we run this command. The outcome is “6” as shown by the statement, which states “division 60 by 10 is: 6.”

Conclusion

This article has discussed how to write a bash function in Linux that takes arguments. In a bash function, various techniques for sending arguments have been defined. In the first section of this article, we covered how to write a script in bash and build a function, as well as how to provide a single argument to a function. The numerous parameters are supplied to a bash function in the second half, and the integer argument is passed in the last section to perform division.

Share Button

Source: linuxhint.com

Leave a Reply