How to Read Array and Print in Bash script

Here’s a Bash script that reads the array and prints the values. On top of that, you can print the index if you need.

Let us go details of Vector and Matrix. A single-value array is called Vector, and if it has multiple Values, it’s called Matrix.

Not only numbers, but you can also have strings in arrays. In real-time, you can find this type of array.

Reading Array

Reading Array

A loop logic you need here to read the elements of the array. So I have used for loop. 

As a first step, I have initialized the numbers with numeric values from 1 to 5. In addition, I have initialized total1 and total2 to ‘0’.

First, for loop reads all the elements. Second, the total1 appends array elements with ‘0’. 

The let command do computation(It sums up all the numbers and gives a count). So it describes the function of the script.

Result of Array Script

Result of Array Script

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.