Array Basics in Shell Scripting | Set 1

Consider a Situation if we want to store 1000 numbers and perform operations on them. If we use simple variable concept then we have to create 1000 variables and the perform operations on them. But it is difficult to handle a large number of variables. So it is good to store the same type of values in the array and then access via index number.

Array in Shell Scripting
An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable which contains multiple values may be of same type or different type since by default in shell script everything is treated as a string. An array is zero-based ie indexing start with 0.

How to Declare Array in Shell Scripting?
We can declare an array in a shell script in different ways.

1. Indirect Declaration
In Indirect declaration, We assigned a value in a particular index of Array Variable. No need to first declare.

ARRAYNAME[INDEXNR]=value

2. Explicit Declaration
In Explicit Declaration, First We declare array then assigned the values.

declare -a ARRAYNAME

3. Compound Assignment
In Compount Assignment, We declare array with a bunch of values. We can add other values later too.

ARRAYNAME=(value1 value2  .... valueN)

or
[indexnumber=]string

ARRAYNAME=([1]=10 [2]=20 [3]=30)

To Print Array Value in Shell Script?

To Print All elements
[@] & [*] means All elements of Array.

echo ${ARRAYNAME[*]}
filter_none

edit
close

play_arrow

link
brightness_4
code

#! /bin/bash
 
# To declare static Array 
arr=(prakhar ankit 1 rishabh manish abhinav)
 
# To print all elements of array
echo ${arr[@]}       
echo ${arr[*]}       
echo ${arr[@]:0}    
echo ${arr[*]:0}    

chevron_right


Output:

prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav

To Print first element

filter_none

edit
close

play_arrow

link
brightness_4
code

# To print first element
echo ${arr[0]}        
echo ${arr}        

chevron_right


Output:

prakhar
prakhar

To Print Selected index element

echo ${ARRAYNAME[INDEXNR]}
filter_none

edit
close

play_arrow

link
brightness_4
code

# To print particular element
echo ${arr[3]}        
echo ${arr[1]}        

chevron_right


Output:

rishabh
ankit

To print elements from a particular index

echo ${ARRAYNAME[WHICH_ELEMENT]:STARTING_INDEX}
filter_none

edit
close

play_arrow

link
brightness_4
code

# To print elements from a particular index
echo ${arr[@]:0}     
echo ${arr[@]:1}
echo ${arr[@]:2}     
echo ${arr[0]:1}    

chevron_right


Output:

prakhar ankit 1 rishabh manish abhinav
ankit 1 rishabh manish abhinav
1 rishabh manish abhinav
prakhar

To print elements in range

echo ${ARRAYNAME[WHICH_ELEMENT]:STARTING_INDEX:COUNT_ELEMENT}
filter_none

edit
close

play_arrow

link
brightness_4
code

# To print elements in range
echo ${arr[@]:1:4}     
echo ${arr[@]:2:3}     
echo ${arr[0]:1:3}    

chevron_right


Output:

ankit 1 rishabh manish
1 rishabh manish
rak

To count Length of in Array

To count the length of a particular element in Array.
Use #(hash) to print length of particular element

filter_none

edit
close

play_arrow

link
brightness_4
code

# Length of Particular element
echo ${#arr[0]}        
echo ${#arr}        

chevron_right


Output:

7
7

To count length of Array.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Size of an Array
echo ${#arr[@]}        
echo ${#arr[*]}        

chevron_right


Output:

6
6

To Search in Array

arr[@] : All Array Elements.
/Search_using_Regular_Expression/ : Search in Array
Search Returns 1 if it found the pattern else it return zero. It does not alter the original array elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Search in Array
echo ${arr[@]/*[aA]*/}    

chevron_right


Output:

1

To Search & Replace in Array

//Search_using_Regular_Expression/Replace : Search & Replace

Search & Replace does not change in Original Value of Array Element. It just returned the new value. So you can store this value in same or different variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Replacing Substring Temporary
echo ${arr[@]//a/A}         
echo ${arr[@]}             
echo ${arr[0]//r/R}        

chevron_right


Output:

prAkhAr Ankit 1 rishAbh mAnish AbhinAv
prakhar ankit 1 rishabh manish abhinav
RakhaR

To delete Array Variable in Shell Script?

To delete index-1 element

unset ARRAYNAME[1]

To delete the whole Array

unset ARRAYNAME
filter_none

edit
close

play_arrow

link
brightness_4
code

#! /bin/bash
# To declare static Array 
arr=(prakhar ankit 1 rishabh manish abhinav)
  
# To print all elements of array
echo ${arr[@]}        # prakhar ankit 1 rishabh manish abhinav
echo ${arr[*]}        # prakhar ankit 1 rishabh manish abhinav
echo ${arr[@]:0}    # prakhar ankit 1 rishabh manish abhinav
echo ${arr[*]:0}    # prakhar ankit 1 rishabh manish abhinav
  
# To print first element
echo ${arr[0]}        # prakhar
echo ${arr}            # prakhar
  
# To print particular element
echo ${arr[3]}        # rishabh
echo ${arr[1]}        # ankit
  
# To print elements from a particular index
echo ${arr[@]:0}    # prakhar ankit 1 rishabh manish abhinav
echo ${arr[@]:1}    # ankit 1 rishabh manish abhinav
echo ${arr[@]:2}    # 1 rishabh manish abhinav
echo ${arr[0]:1}    # rakhar
  
# To print elements in range
echo ${arr[@]:1:4}    # ankit 1 rishabh manish
echo ${arr[@]:2:3}    # 1 rishabh manish
echo ${arr[0]:1:3}    # rak
  
# Length of Particular element
echo ${#arr[0]}        # 7
echo ${#arr}        # 7
  
# Size of an Array
echo ${#arr[@]}        # 6
echo ${#arr[*]}        # 6
  
# Search in Array
echo ${arr[@]/*[aA]*/}    # 1
  
# Replacing Substring Temporary
echo ${arr[@]//a/A}        # prAkhAr Ankit 1 rishAbh mAnish AbhinAv
echo ${arr[@]}            # prakhar ankit 1 rishabh manish abhinav
echo ${arr[0]//r/R}        # pRakhaR

chevron_right


Output:

prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar
prakhar
rishabh
ankit
prakhar ankit 1 rishabh manish abhinav
ankit 1 rishabh manish abhinav
1 rishabh manish abhinav
rakhar
ankit 1 rishabh manish
1 rishabh manish
rak
7
7
6
6
1
prAkhAr Ankit 1 rishAbh mAnish AbhinAv
prakhar ankit 1 rishabh manish abhinav
pRakhaR

Reference:
https://www.tecmint.com/working-with-arrays-in-linux-shell-scripting/



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.