Array Basics in Shell Scripting | Set 1 - GeeksforGeeks

30 Jan, 2018
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 ScriptingAn 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 DeclarationIn Indirect declaration, We assigned a value in a particular index of Array Variable. No need to first declare.
ARRAYNAME[INDEXNR]=value
2. Explicit DeclarationIn Explicit Declaration, First We declare array then assigned the values.
declare -a ARRAYNAME
3. Compound AssignmentIn 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[*]}
#! /bin/bash # To declare static Array arr=(prakhar ankit 1 rishabh manish abhinav) # To print all elements of arrayecho ${arr[@]}       echo ${arr[*]}       echo ${arr[@]:0}    echo ${arr[*]:0}    
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
# To print first elementecho ${arr[0]}        echo ${arr}        
Output:
prakhar
prakhar

To Print Selected index element
echo ${ARRAYNAME[INDEXNR]}
# To print particular elementecho ${arr[3]}        echo ${arr[1]}        
Output:
rishabh
ankit

To print elements from a particular index
echo ${ARRAYNAME[WHICH_ELEMENT]:STARTING_INDEX}
# To print elements from a particular indexecho ${arr[@]:0}     echo ${arr[@]:1}echo ${arr[@]:2}     echo ${arr[0]:1}    
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}
# To print elements in rangeecho ${arr[@]:1:4}     echo ${arr[@]:2:3}     echo ${arr[0]:1:3}    
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
# Length of Particular elementecho ${#arr[0]}        echo ${#arr}        
Output:
7
7

To count length of Array.
# Size of an Arrayecho ${#arr[@]}        echo ${#arr[*]}        
Output:
6
6

To Search in Array
arr[@] : All Array Elements./Search_using_Regular_Expression/ : Search in ArraySearch Returns 1 if it found the pattern else it return zero. It does not alter the original array elements.
# Search in Arrayecho ${arr[@]/*[aA]*/}    
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.
# Replacing Substring Temporaryecho ${arr[@]//a/A}         echo ${arr[@]}             echo ${arr[0]//r/R}        
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
#! /bin/bash# To declare static Array arr=(prakhar ankit 1 rishabh manish abhinav)  # To print all elements of arrayecho ${arr[@]}        # prakhar ankit 1 rishabh manish abhinavecho ${arr[*]}        # prakhar ankit 1 rishabh manish abhinavecho ${arr[@]:0}    # prakhar ankit 1 rishabh manish abhinavecho ${arr[*]:0}    # prakhar ankit 1 rishabh manish abhinav  # To print first elementecho ${arr[0]}        # prakharecho ${arr}            # prakhar  # To print particular elementecho ${arr[3]}        # rishabhecho ${arr[1]}        # ankit  # To print elements from a particular indexecho ${arr[@]:0}    # prakhar ankit 1 rishabh manish abhinavecho ${arr[@]:1}    # ankit 1 rishabh manish abhinavecho ${arr[@]:2}    # 1 rishabh manish abhinavecho ${arr[0]:1}    # rakhar  # To print elements in rangeecho ${arr[@]:1:4}    # ankit 1 rishabh manishecho ${arr[@]:2:3}    # 1 rishabh manishecho ${arr[0]:1:3}    # rak  # Length of Particular elementecho ${#arr[0]}        # 7echo ${#arr}        # 7  # Size of an Arrayecho ${#arr[@]}        # 6echo ${#arr[*]}        # 6  # Search in Arrayecho ${arr[@]/*[aA]*/}    # 1  # Replacing Substring Temporaryecho ${arr[@]//a/A}        # prAkhAr Ankit 1 rishAbh mAnish AbhinAvecho ${arr[@]}            # prakhar ankit 1 rishabh manish abhinavecho ${arr[0]//r/R}        # pRakhaR
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/
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
tar command in Linux with examples
curl command in Linux with Examples
UDP Server-Client implementation in C
Conditional Statements | Shell Script
Cat command in Linux with examples
echo command in Linux with Examples
touch command in Linux with Examples
Tail command in Linux with examples
Compiling with g++