Bash Print Array

Bash Print Array

Bash Print Array [ Entire array]

There are multiple ways to print array in Bash; let’s learn them below.

Use echo with Parameter Expansion

Use the echo command with parameter expansion syntax to print the array on the Bash console.

Alternatively, we can print the array as follows:

In both examples, we created an array named fruits containing different fruit names. Then, we used the echo command with two variations of parameter expansion syntax ("${fruits[*]}" and "${fruits[@]}") to display the entire array on the Bash console. If both parameter expansion syntaxes produced the same results, why did we use both? Because there is a subtle difference between them.

According to the Oreilly, both syntaxes are used to expand all the items of the specified array, which was fruits in our case. The only difference between these two parameter expansion syntaxes is how they can handle the whitespace characters in the fruits array elements.

The "${fruits[*]}" expanded the array elements into a single word, where each element was separated by the first character of the IFS (Internal Field Separator) variable. By default, the IFS was initialized to a whitespace character, so if any array items contained whitespace, they would be split into separate words.

On the other hand, the "${fruits[@]}" expanded the items/elements of the array into separate words, where each element was taken as a separate word. This meant that whitespace characters in the array elements were preserved.

See the following example to clarify the difference between the "${fruits[*]}" and "${fruits[@]}".

So, the subtle difference between "${fruits[*]}" and "${fruits[@]}" was that the former expanded the array elements into a single word, while the latter expanded them into separate words.

Are you bound to use the "${fruits[*]}" parameter expansion syntax but want to join array elements with a custom delimiter? See the following example.

Use the echo command with parameter expansion syntax to print the entire array by joining them with a custom delimiter.

As we already learned that the "${fruits[*]}" syntax expanded the array elements into a single word using the default value of IFS variable as a delimiter, but in the above example, we explicitly modified the value of IFS variable from a whitespace character to a comma. Note that the ; was used to separate commands in the substitution syntax ($()).

Finally, we captured the output using substitution syntax and assigned it to the joinedArr variable, which we further used with the echo command to print the array elements on the Bash console.

Use printf with the Parameter Expansion

Use the printf command with parameter expansion syntax to print the entire array on the Bash console.

Alternatively, we can use as follows:

Again, we created an array containing fruits’ names as array elements. Then, we used the printf command to display the elements of the fruits array on the Bash console. Note that the printf command formats and prints the text.

In the printf statement, the %s denoted that the corresponding item would be treated as a string, while \n was an escape sequence, which we used to print a newline character after each element of the fruits array.

The worth noting point was that both ("${fruits[@]}" and "${fruits[*]}") expanded the fruits array elements, but we got one-liner output using "${fruits[*]}", while each element of the array was on a separate line using "${fruits[@]}". Why? Because the ${fruits[*]} parameter expansion syntax expanded the array elements into a single word while ${fruits[@]} expanded them into separate words.

Using double quotes with both variants of the parameter expansion syntax as "${array_name[*]}" and "${array_name[@]}" will preserve the array structure but still expand the array items to a single word and separate elements respectively.

Use declare Command

Use the declare command with the -p parameter to print the entire array on the Bash console.

Here, we used the declare command. The declare command with the -p option displayed the attributes and values of the fruits variable. The -p option was used to show the options and attributes of the variable; omitting it will not display anything.

In the output (you can see above), the -a option indicated that the fruits variable was an indexed array where the first index was 0 and the last was the arrayLength-1.

Use for Loop

Use the for loop with parameter expansion to iterate over the array to print the entire array on the Bash console.

We already learned about "${fruits[@]}", while the item variable was used to access the current item from the expanded array. In the above example, do meant the start of the for loop’s body, whereas the done marked the end of the loop.

Use the for loop with the array index to iterate over the array to print the entire array on the Bash console.

Again, we used a for loop to iterate over the array and print its elements on the Bash console. We used the i variable to trace the counter, which ensured the current value of i is less than (<) the size of the array. Here, the ${#fruits[@]} returned the size of the fruits array.

For each value of i, we retrieved the corresponding element from the fruits array and printed it on the console using the echo command.

Until now, we learned various ways to print the entire array; what if you want to print one or multiple elements from the array? Let’s understand it below.

Bash Print One/Multiple Array Elements

There are multiple ways to print one or multiple elements of an array in Bash; let’s explore them below. Note that you must understand how the parameter expansion syntax work to use any of the following approaches.

Use Index

Use the index to print the specific element of the array in Bash.

As the fruits array was an indexed array, it followed a zero-based index system, so specifying the 2 retrieved the Banana as an output.

Use Array Slicing

Use array slicing o print the multiple elements of the array in Bash.

Here, the specified indexes 1 and 3 are inclusive.

We can also print all the array elements starting from a particular index.

Use for Loop

Use the for loop to print multiple array elements but not all. This approach is useful if you want to retrieve items from different indices.

In the above code snippet, we created two arrays: fruits containing fruit names as elements and indices having indices. Then, we used a for loop to iterate over the indices array; for each index in the indices, we got the element from the fruits array, which we printed using the echo command.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *