Array and Bash shell

Array and Bash shell

Introduction – Array is one of the best feature of Bash shell and  I am fan of this feature!!. Bash shell provides one-dimensional array. Any variable may be used as an array. There are many ways to declare array. Builtin “declare” command is used to explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based. A array is created automatically if any variable is assigned to using the syntax array_name[subscript]=value. The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero.  To explicitly declare an array, use “declare  -a  name“. “declare -a name[index]” is also accepted and the “index” is ignored here.

Arrays are assigned to using compound assignments of the form array_name=(value1,value2, … valueN), where each value is of the form [index]=string. Only string  is required. In bash array indexing is same as C language which starts at zero. This syntax is also accepted by the declare builtin.

* How to assign/declare array variable in Bash shell –

declare -a myarr[0]=”Arun”
declare -a  my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
apna_array=([0]=arun [1]=bagul [4]=Santhosh [3]=ravi [5]=1024)
array[1]=”Bagul”

* How to refer the array and it’s element –

Suppose I want to print the above array “apna_array”, the syntax as shown below….

echo “${apna_array[*]}”
and
echo “${apna_array[@]}”

This syntax is very common in bash shell, ${variable_name} is same as $variable_name. While in array the “[*]” and “[@]” indicate the all element of array.
NOTE – Remember above two  syntax are not same there is one difference. It’s an assignment for you guys/girls!

* How to access particular element of array –

name=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)

here “name” is an array and I want to access the first and second element of the array “name”.

echo “My name is – ${name[0]}  ${name[1]}”

* How to find the length of array or element of array –

${#my_array[index]} expands to the length of (element of array) ${my_array[index]} . If “index” is “*” or “#” then expansion is the number of elements in the array. Please note – referencing an array variable without a subscript/index is equivalent to referencing element zero.

Suppose “student” is array then, ${student} is same as ${student[0]}

#declare and assign elements to array “student_record” –
student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)

echo ${student_record}
echo ${student_record[0]}

echo “Total no of element in array is – ${#student_record[*]}”
echo “Total no of element in array is – ${#student_record[@]}”

echo “Length of element is – ${#student_record[1]}”

* How to delete element of array –

unset”  builtin command is used to delete element of array.

unset array_name[index]  – delete the array element at given “index”. If “index” is “*” or “@” then, it will removes the entire array.

big_city=(Bangalore Pune Nasik Hydrabad Surat)

Here “big_city” is the array of big cities in India. Suppose I want to delete “Nasik” from above array –

unset big_city[2]

* Example – Look below script and it’s output

root@arun:~# cat  how_to_array.sh
#!/bin/bash

#declare and assign element
declare -a myarr[0]=”Arun”
declare -a metro_array=(Mumbai Delhi Kolkata Chennai)
declare -a  my_array
my_array=(Arun Bagul Bangalore Mumbai Raju Santhosh Ravi)
name_arr[1]=”Arun Bagul”

echo “————————–”
echo “myarr is – ${myarr[0]}”
echo “Metro cities in India – ${metro_array[*]}”
echo “My name is – ${my_array[0]}  ${my_array[1]}”

echo “Name – ${name_arr[1]}”
echo “Name – ${name_arr[*]}”

echo “————————–”

#how to assign element in array
myarr[1]=”- System Engineer!”
echo “myarr is – ${myarr[*]}”

apna_array=([0]=arun [1]=bagul [3]=Santhosh [4]=ravi [5]=1024)

echo “${apna_array[*]}”
echo “${apna_array[@]}”

student_record=(10 “Ravi Bhure” “1st year of BE” “SSVPS College” Dhule)

echo ${student_record}
echo ${student_record[0]}

echo “Total no of element in array is – ${#student_record[*]}”
echo “Total no of element in array is – ${#student_record[@]}”
echo “Length of element is – ${#student_record[1]}”

#how to delete array –
del_arr=(Arun Bagul)
echo “del_arr before ‘delete’ is – ${del_arr[*]}”
#delete
unset del_arr

echo “del_arr after ‘delete’ is – ${del_arr[*]}”

echo “————————–”

#how to delete one element in array –

big_city=(Bangalore Pune Nasik Hydrabad Surat)

#suppose – want to delete “Nasik” from above array –

echo “Big City before ‘delete’ is – ${big_city[*]}”

#delete
unset big_city[2]

echo “Big city after ‘delete’ is – ${big_city[*]}”

root@arun:~#

*** Run above script –

root@arun:~# ./how_to_array.sh
————————–
myarr is – Arun
Metro cities in India – Mumbai Delhi Kolkata Chennai
My name is – Arun  Bagul
Name – Arun Bagul
Name – Arun Bagul
————————–
myarr is – Arun – System Engineer!
arun bagul Santhosh ravi 1024
arun bagul Santhosh ravi 1024
10
10
Total no of element in array is – 5
Total no of element in array is – 5
Length of element is – 10
del_arr before ‘delete’ is – Arun Bagul
del_arr after ‘delete’ is –
————————–
Big City before ‘delete’ is – Bangalore Pune Nasik Hydrabad Surat
Big city after ‘delete’ is – Bangalore Pune Hydrabad Surat
root@arun:~#

Thank you,
Arun Bagul

Similar Posts:

Leave a Reply

Your email address will not be published.