BASH ARRAY – In this shell tutorial, you will learn how to create an array for homogenous and heterogeneous type, index based array creation, displaying linux commands using array.
- Array is a set of elements which are having same type or different type
- It is an example of linear data structures (sequential data structure)
- It is accessed by the index number which starts from 0 to n-1
- It supports the storage, retrieval, insertion, deletion and updation
- In shell, the array can be created with help of the special operator symbol () instead of square operator []
- Unlike other compiled languages c/c++/c#/java, the shell follows the dynamic data typed system. So no need to mention the data type in the creation of an array.
Syntax
Where,
- Element 1, Element 2, …Element n can be Same Type or Different Type.
Example
Length
- Array length can be done by using the special symbol # followed by @ or * symbol along with array name
- It is very important to note that the curly braces operators {} are used for accessing the array in the shell.
Syntax
Example
Accessing Individual array contents
- The contents of the array can have accessed by using its index location
Example
Printing Individual Element
- The individual element of an array can be displayed using the curly braces {} along with the array name.
Syntax
Example
Accessing Entire array contents
- The entire array elements can be called using the star symbol * or @ with array name.
- It is an important to note that, if the index number is * or @, the whole elements of the array are referenced.
Example
I. EXAMPLE OF ARRAY CREATION FOR HOMOGENOUS TYPE
(/home/runner/Latest-Shells-21/arr1.sh)
1. SOURCE CODE
2. OUTPUT
II. EXAMPLE OF ARRAY CREATION FOR HETEROGENEOUS TYPE
(/home/runner/Latest-Shells-21/arr2.sh)
1. SOURCE CODE
echo “————————————————-“
echo “Shell Array for Different Type of Elements”
echo “————————————————-“
# Array Creation
lb=(“Shiva” 21 72.93 True)
# print the contents of array at the same time
echo “Array Contents using name”
echo ${lb[*]}
# print the length of the array
echo “Length of the Array: ${#lb[@]}”
echo “Array Contents using for loop”
echo “————————————————-“
# print the contents of array
for i in ${lb[*]}
do
echo $i
done
2. OUTPUT
III. INDEX BASED ARRAY CREATION FOR HETEROGENEOUS TYPE
(/home/runner/Latest-Shells-21/arr3.sh)
1. SOURCE CODE
echo “————————————————-“
echo “Array Creation using Index”
echo “————————————————-“
# Empty array
ls=()
# store the elements to array using index number
ls[0]=12
ls[1]=21
ls[2]=’M’
ls[3]=True
ls[4]=”Rohit”
# print the contents of array at the same time
echo “Array Contents using name”
echo ${ls[*]}
# print the length of the array
echo “Length of the Array: ${#ls[@]}”
echo “Array Contents using for loop”
echo “————————————————-“
# print the contents of array
for i in ${ls[*]}
do
echo $i
done
2. OUTPUT
IV. DISPLAYING LINUX COMMANDS USING ARRAY
(/home/runner/Latest-Shells-21/arr4.sh)
1. SOURCE CODE
echo “————————————————-“
echo “Shell Array for Same Type of Elements”
echo “————————————————-“
# Array Creation
lb=()
# assigning linux commands to the each location of array
lb[0]=$(ls)
lb[1]=$(date)
lb[2]=`cal`
lb[3]=`cat /etc/shells`
# print the length of the array
echo “Length of the Array: ${#lb[@]}”
echo “Array Contents using for loop”
echo “————————————————-“
# print the contents of array
for i in ${lb[*]}
do
echo $i
done
2. OUTPUT
2.1 DISPLAYING THE OUTPUT OF LINUX COMMANDS
2.2 DISPLAYING THE OUTPUT OF LINUX COMMANDS – (CONTINUE)
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…