---
title: "How to Read Array and Print in Bash script"
description: "Here's a simplified script to read all the elements of the array. You can do two things with this script. Those are appending and Counting."
url: https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/
date: 2021-10-23
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2021/10/pexels-photo-8419213.jpeg?fit=1880%2C1253&ssl=1
categories: ["Shell scripts"]
tags: ["Bash script of Arrays"]
type: post
lang: en
---

# 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.

Related[The Easy Way to get Array Data in Shell-Script](https://srinimf.com/2022/10/10/the-easy-way-to-get-array-data-in-shell-script/)

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](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/10/script-1.jpg?resize=726%2C483&ssl=1)

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](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/10/result.jpg?resize=727%2C377&ssl=1)

Related Posts

- [The Easy Way to get Array Data in Shell-Script](https://srinimf.com/2022/10/10/the-easy-way-to-get-array-data-in-shell-script/)DateOctober 10, 2022
- [Bash Script Language Fundamentals: Learn How to Automate Tasks](https://srinimf.com/2023/07/29/how-to-write-shell-script-unlock-the-commands/)DateJuly 29, 2023
- [How to Call a Shell Script from Another Script](https://srinimf.com/2021/11/01/heres-the-real-reason-to-include-script-in-other-script/)DateNovember 1, 2021

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2021%2F10%2F23%2Fbash-script-simplified-how-to-read-array-and-print%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2021%2F10%2Fpexels-photo-8419213.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=How%20to%20Read%20Array%20and%20Print%20in%20Bash%20script)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Read%20Array%20and%20Print%20in%20Bash%20script&body=https%3A%2F%2Fsrinimf.com%2F2021%2F10%2F23%2Fbash-script-simplified-how-to-read-array-and-print%2F&share=email)
-

### Like this:

Like Loading…
