Introduction to Array in Unix
Array in Unix is a data structure consisting of a collection of elements stored in a memory location which is contiguous, whose data type can be the same or different like integer, strings, etc. and they are referenced by the index beginning with zero. So, array in Unix is called to be zero-based which is formed either by indirect declaration or explicit declaration or compound assignment and there is no limit on the array size.
The array data structure is also available in Unix.
- Array in Unix: It is the collection of elements that may or may not be of the same datatype.
- Index: Array is zero-based, which means elements of the array are referenced by the index starting zero.
- Size: There is no maximum limit on the size of the array
Syntax of Array in Unix
There are different ways of forming an array in shell scripting. Let us go through each one of them in details:
1. Indirect Declaration: Here value is assigned for a particular index on the go. Example of which is mentioned below.
Syntax:
array_name[index] = value
2. Explicit Declaration: First, the array is declared and then later the values are assigned to it. Declare is built-in keyword and -a is an option of reading built-in which allows reading and assigning values.
Syntax:
declare -a array_name
3. Compound Assignment: Here, the array is declared with multiple values at a time.
Syntax:
array_name = (value1 value2 value3 . . . valueN)
Or
array_name = ([0]=value1, [1]=value2,[2]=value3..)
Here the index is optional if the index is not provided the value is assigned to the last index plus one
How Does Array Work in Unix?
We have learned the various ways of creating an array in Unix, let us now move forward and see various operations that can be performed on the Unix array.
1. We will create the array of names
2. To access all the elements of the array use either [*] or [@]
Code:
echo ${first_name[*]}
echo ${first_name[@]}
Syntax:
echo [options] [arguments]
3. To access any specific element of the string using its index.
Code:
echo ${first_name[0]}
echo ${first_name[1]}
echo ${first_name[2]}
Output:
4. To print the elements in a range. The syntax for the same is as follows:
Syntax:
echo $array_name[which element]:starting_index:count_element]
Let us familiarize ourselves with the same.
Code:
echo ${first_name[@]:0:2}
echo ${first_name[@]:2:3}
echo ${first_name[0]:1:2}
Output:
@ – refers to all the elements of the array
In the command echo ${first_name[@]:0:2}
@ consider all the elements of the array. 0 – means to start with this particular index and 2 displays the number of elements from the starting point
2 displays a number of elements from the starting point
In this command echo ${first_name[0]:1:2}
0 – consider the 0th element of the array. 1- is the starting point and 2 – is the count of the number of elements from the starting point
5. To get the size of the array
Code:
echo ${#first_name[@]}
or
echo ${#first_name[*]}
Output:
6. To find the length of a specific element of an array
Code:
echo ${#first_name[0]}
Output:
7. To get the index of the array to use the below command.
Code:
echo ${!first_name[@]}
Output:
8. To delete the array in the script unset command is used. It is a built-in command to destroy an array or any element of the array
Syntax:
unset array_name
Code:
unset first_name
9. To delete an element at any specific index
unset first_name[2]
This will delete the element at index 2.
10. To search for a specific pattern in the array.
Code:
echo ${first_name[@]/*[aA]*/}
Output:
Here, first_name[@]: refers to all the elements of the array
/pattern to be searched/: It is the pattern that is to be searched in the array or its element.
It will return 1 if the match is found and 0 if no match found.
11. To search for a specific pattern and replace it in the given array.
Code:
echo ${first_name[@]//a/A}
first_name[@]: Consider all the elements of the array
//pattern to be searched/replacement string/: Search & replacement string
Output:
first_name[2]: Search and replace operations to be performed on the element with index 2.
While performing the search and replace operation there are no changes made in the original value of the array. It returns a new value that can be stored in the same or different variables.
12. To perform an operation on the elements of an array we can use the Loops. Like any other programming language, Bourne shell supports two types of loops i.e. for loop and while loop. Let us look at an example to iterate over an array using for loop.
Code:
Arr = (1 2 3 4 5)
for i in "${Arr[@]}"
do
echo $i
done
Output:
Similarly, you can iterate over an array using a while loop as well.
Please keep in mind that there are various different types of shells available in Unix like Bash, K shell, Bourne shell, etc. Shell is nothing but an interface to Unix systems with different sets of commands and functions. So, based on various types of shell there are different syntax associated with the array and its operations with a slight variation in the syntax of the commands that are discussed above.
Bash Shell permits the array operations on the variables as well without the variables being explicitly declared as an array. Let us look at the example to have a better understanding of the above statement.
Code:
String_variable = India is a democratic country
echo "${string[0]}"
Output:
Conclusion
Basically array is a collection of items stored in a contiguous memory location. The purpose behind forming an array is to store multiple items of the same type together with the same or different data types. In a practical scenario why array structure lags popularity is due to its lack of compatibility with various shell and complex structures.
Recommended Articles
This is a guide to Array in Unix. Here we discuss the syntax and working of array in Unix along with various examples and code implementation. You may also look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses