Working with Arrays in Linux Shell Scripting – Part 8

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Anoop C S

I am basically a FOSS enthusiast interested in working under GNU/Linux and system administration. Looking forward to become a part of an open source initiative. Currently pursue Computer Science & Engineering.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

Receive Your Free Complimentary eBook NOW! -

Download Free Linux eBooks

Linux BASH Programming Cookbook
Getting Started with Ubuntu 16.04
A Newbie's Getting Started Guide to Linux
Introduction to Linux - A Hands on Guide

You may also like...

10 Responses

  1. Gulab says:

    how to import multiple directory in array(run time log /var/log/ ) in runtime and check if directory is present or not ?

    • Anoop C S says:

      Hi Gulab,

      Sorry for being late..Got stuck with some other work. I would assume that by this time you might have found an answer to your question. Anyway following is my pov.

      You can always insert elements into array by referring to the index numbers as I have mentioned in the article. By run time if you mean to have some kind of looping, then you could increment index number for the array within the loop and insert elements one by one. Checking for directory existence is not at all a complex task and you have various methods to do so (easy way: use if conditional statement with test expression parameter -d).

  2. BV Naagabaabu says:

    is there any way to change the default index value 0 of array to 1..

    • Anoop C S says:

      Can you please specify the use case?

      • BV Naagabaabu says:

        actually we can access an array element like below

        echo array_name[0]

        the above echo command prints “name_1”

        but i would like to print name_1 by using below echo command
        echo array_name[1]

        Here my intention is change the array default index value 0 to 1. so that i can print first value of array by using array_name[1] instead of using array_name[0]

        • Anoop C S says:

          Sorry for the late reply:

          Some corrections to your example mentioned below:
          * Your array initialization is wrong. I assume your intention was to initialize array as follows,
          # array_name=(name_1 name_2 name_3 name_4 name_5)
          Array elements are by default separated by one or more white spaces.

          * Your de-referencing of array elements is wrong. See the correct usage below,
          # echo ${array_name[0]}

          Now coming to your question:
          Yes, it is possible. You need to initialize the array by referencing the index as,
          # array_name=([1]=name_1 name_2 name_3 name_4 name_5)
          This means
          array_name[1] = name_1
          array_name[2] = name_2
          array_name[3] = name_3
          array_name[4] = name_4
          array_name[5] = name_5

          Hope this solution is helpful.

          • Thanks for your reply. I got it what exactly i want. Actually i’m facing some issues while using arrays in for loop and while loop. In those for and while loops the loop count is starting from 1, at that time if i want to use an array inside that loop, then i need to increase array index value to 1 manually. Now your solution solves this. Now i can directly use array with in a loop which has loop count start from 1.


  3. Syed Aqhib says:


  4. skumar says:

    Your examples are so good. Thanks.

  5. santosh loke says:

    nice to know the functionality of arrays.

Got something to say? Join the discussion.

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

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?