Shell Programming Blog Shell Programming Blog

Tuesday 25 September 2012

How to use Arrays in Shell scripts

Unknown | 10:28 |


We know what an array is. An array is a variable which can store multiple values in the same name. These values can be accessed by using the subscripts. The whole of the array can be accessed by just mentioning the array name without any subscripts. Let us see some of the important operations using array in BASH with the help of the below script.




The output of the above script would be

$ ./array.sh
The array contains 4 members. They are:
0: ram
1: rick
2: sittik
3: rahim
Adding bill to the end of the array
Listing all the elements in the array
ram rick sittik rahim bill
Listing all the elements in the array
ram rick sittik rahim bill
Deleting "rick"
Now the array is
ram sittik rahim bill
length of 3rd element in the array
6
Deleting the whole array



2 comments:

  1. Hi

    While running your above program by copying and pasting, i got error in line:5. Could not able to resolve it. But it seems there is some syntax error.

    ./array_program: 5: ./array_program: Syntax error: "(" unexpected


    Could you please correct it ?

    Regards
    Tom

    ReplyDelete
    Replies
    1. got the answer. I was running it in /bin/sh. But it should be /bin/bash

      Thank you

      Regards
      Tom

      Delete

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming