Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
In Bash, passing individual variables to functions as arguments is pretty straightforward. However, when we work with arrays, there’s more to consider.
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.
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:
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.
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.
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.