Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Create and Use Array in Bash Script

    How to Create and Use Array in Bash Script

    By RahulJuly 24, 20192 Mins Read

    An array is a data structure consist multiple elements based on key pair basis. Each array element is accessible via a key index number.

    Advertisement

    This tutorial will help you to create an Array in bash script. Also, initialize an array, add an element, update element and delete an element in the bash script.

    Define An Array in Bash

    You have two ways to create a new array in bash script. The first one is to use declare command to define an Array. This command will define an associative array named test_array.

    declare -a test_array
    

    In another way, you can simply create Array by assigning elements.

    test_array=(apple orange lemon)
    

    Access Array Elements

    Similar to other programming languages, Bash array elements can be accessed using index number starts from 0 then 1,2,3…n. This will work with the associative array which index numbers are numeric.

    echo ${test_array[0]}
    
    apple
    

    To print all elements of an Array using @ or * instead of the specific index number.

    echo ${test_array[@]}
    
    apple orange lemon
    

    Loop through an Array

    You can also access the Array elements using the loop in the bash script. A loop is useful for traversing to all array elements one by one and perform some operations on it.

    1
    2
    3
    4
    for i in ${test_array[@]}
    do
      echo $i
    done

    Adding New Elements to Array

    You can add any number of more elements to existing array using (+=) operating. You just need to add new elements like:

    test_array+=(mango banana)
    

    View the array elements after adding new:

    echo ${test_array[@]}
    
    apple orange lemon mango banana
    

    Update Array Elements

    To update the array element, simply assign any new value to the existing array by the index. Let’s change the current array element at index 2 with grapes.

    test_array[2]=grapes
    

    View the array elements after adding new:

    echo ${test_array[@]}
    
    apple orange grapes mango banana
    

    Delete An Array Element

    You can simply remove any array elements by using the index number. To remove an element at index 2 from an array in bash script.

    unset test_array[2]
    

    View the array elements after adding new:

    echo ${test_array[@]}
    
    apple orange mango banana
    

    array bash script shell
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    View 6 Comments

    6 Comments

    1. Marcelo Miranda on September 17, 2020 12:02 pm

      I am seeing lots of webpages showing how to operate ARRAYS on Bash with Strings but… how to operate them with NUMBER?
      And (worst) how to POPULATE then with these numbers from it being initially EMPTY?
      And (once more worst) how to populate them with variables that carry numbers (not strings)?

      Reply
      • Jeevan Sirkunan on August 19, 2021 4:42 am

        declare -a array=( $(for i in {1..10}; do echo 0; done) ) # empty array
        j=0

        for i in ${array[@]}
        do
        echo $i
        done

        # numbered array
        for i in ${array[@]}
        do
        array[j]=$j
        j=$((j+1))
        done

        for i in ${array[@]}
        do
        echo $i
        done

        Reply
    2. Hugo Fittipaldi on September 7, 2019 2:15 pm

      Hi, how do I add “green apple” to this array?

      Reply
      • Rahul on September 10, 2019 6:53 am

        Hi, You can use following syntax:

        test_array=(“green apple” orange lemon)

        Reply
    3. Mike on July 23, 2019 11:56 pm

      Loop through an Array, typo instead of `don` should be `done`

      Reply
      • Rahul on July 24, 2019 4:48 am

        Thanks Mike, Article has been updated correctly.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.