1. Overview
In Linux, using functions to store reusable code is not uncommon. Therefore, using arguments to pass inputs to functions is an important concept to learn.
In this tutorial, we’ll learn how to pass an array as an argument to a function in a Bash script.
2. Passing an Array to a Bash Script Function
In Bash, passing individual variables to functions as arguments is pretty straightforward. However, when we work with arrays, there’s more to consider.
2.1. Understanding Arrays in Bash
Before we proceed, we must understand the basics of arrays. An array is a collection of elements, in which each element is assigned an index for accessibility:
$ array=(1 2 3 4)
Here, we declare an array named array.
2.2. Passing an Array to a Function
In Bash, a function allows us to combine a set of commands into one code block for reusability:
print_elements() {
local my_array=("$@") # Assign the passed array to a local array variable
# Print array elements
echo "First element: ${my_array[0]}"
echo "Second element: ${my_array[1]}"
echo "Third element: ${my_array[2]}"
echo "Fourth element: ${my_array[3]}"
}
In the example above, we define a function print_elements() that receives an array as its argument:
- local my_array=(“$@“) – creates a local copy of our argument. As a result, any changes that we make to the local array don’t reflect on the original array outside our function. The special variable “$@” ensures that each array element is treated as a separate argument.
- echo “First element: ${my_array[i]}” – selects an element from the array and prints it out using the echo command. This command works for all the elements in the array based on the index i provided with my_array.
Next, let’s pass the array we previously defined as an argument to our current function:
$ print_elements "${array[@]}"
First element: 1
Second element: 2
Third element: 3
Fourth element: 4
From the output, the print_elements() function displays the elements present in the array argument. In addition, the argument “${array[@]}” expands the array elements so that each element is treated separately.
Now, let’s utilize a Bash script to automate these instructions.
2.3. Automating the Instructions in a Bash Script
In this section, we target to transfer our array and function to a Bash script. To begin, we create the script.sh file:
$ touch script.sh
The touch command enables us to generate an empty file from the command line. To emphasize, the file should contain the .sh extension to specify that it’s a Bash script.
Once this is done, let’s open the file using a text editor like nano and paste the contents below:
#!/bin/bash
# Function that receives an array argument
print_elements() {
local my_array=("$@") # Assign the passed array to a local array variable
# Print array elements
echo "First element: ${my_array[0]}"
echo "Second element: ${my_array[1]}"
echo "Third element: ${my_array[2]}"
echo "Fourth element: ${my_array[3]}"
}
# Declare an array
array=(1 2 3 4)
# Call the function and pass an argument of an array
print_elements "${array[@]}"
The first line in the script is known as a shebang. It specifies the shell that should execute our script. In this case, #!/bin/bash instructs Linux that the Bash shell should interpret the script.sh script. In detail, #! notifies Linux that it’s dealing with a shebang line while /bin/bash declares the path to the interpreter. In the next steps, we declare our function, declare an array, and then call the function with an array argument.
The instructions above represent the new content of our script.sh file. At this point, the next step is to save our changes. We achieve this by pressing the Ctrl+o keys. Afterward, pressing Ctrl+x helps us to exit from the nano text editor.
Now, let’s execute our script file:
$ bash script.sh
First element: 1
Second element: 2
Third element: 3
Fourth element: 4
The bash command helps us execute the script file script.sh. As evident, the output is similar to the one we got when directly passing the array to the function.
3. Conclusion
In this article, we learned how to declare an array in Bash. Next, we explored defining a function and passing an array as its argument. Finally, we made it possible to run these tasks from a Bash script.