Working with Arrays in Linux Shell Scripting – Part 8

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  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.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

12 Responses

  1. swww says:

    Good collection of examples, especially command output assigning is nice. I wrote somewhat similar but more extensive article on arrays in BASH. Check it out here:


  2. kumar says:


    I want print only first and third element of the array.

    what is the echo command?

  3. 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).

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


  5. Syed Aqhib says:


  6. skumar says:

    Your examples are so good. Thanks.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.