EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials Unix Tutorial Array in Unix

Array in Unix

Priya Pedamkar
Article byPriya Pedamkar

Updated May 31, 2023

Array in Unix

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 referenced by the index beginning with zero. So, an array in Unix is called zero-based, formed either by indirect declaration, explicit declaration, or compound assignment, and there is no limit on the array size.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The array data structure is also available in Unix.

  • Array in Unix: It is the collection of elements that may or may not be the same data type.
  • Index: Elements in the array reference indices starting at zero, following a zero-based indexing system.
  • Size: There is no maximum limit on the size of the array

Array in Unix eg1

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 detail:

1. Indirect Declaration: Here, a value is assigned for a particular index on the go. An example of it is provided 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 a 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..)

If an index is not provided, the assignment of the value determines the next available index by adding one to the last index.

How Does Array Work in Unix?

We have learned different ways to create an array in Unix. Now, let’s explore various operations that can be performed on the Unix array.

1. We will create an array of names

Array in Unix eg1

2. To access all the elements of the array, use either [*] or [@]

Code:

echo ${first_name[*]}
echo ${first_name[@]}
Note: echo command is a built-in command in Unix that is used to display the text, or the string passed as an argument to the command. The syntax of the ‘echo’ command is as follows.

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:

Array in Unix eg2

Note: You can refer to the content of the array by using curly braces { }.

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:

eg3

@ – 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: 

eg4

Note: To find the length of a specific element in an array, you can use the “#” symbol.

6. To find the length of a specific element of an array

Code:

echo ${#first_name[0]}

Output:

eg5

7. To get the array index, use the below command.

Code:

echo ${!first_name[@]}

Output:

eg6

Note: To obtain the index of an array, you can use the “!” symbol.

8. To delete the array in the script, you can use the unset command. 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:

Array in Unix eg7

Here, first_name[@]: refers to all the elements of the array
/pattern to be searched/: It is the pattern in the array or its element.
If a match is found, it will return 1; otherwise, it will return 0.

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:

Array in Unix eg8

first_name[2]: Search and replace operations on the element with index 2.

Array in Unix eg9

While performing the search and replace operation, the array’s original value remains unchanged. Instead, it returns a new value that you can store in the same or different variables.

12. To operate on an array’s elements, we can use the Loops. Like any other programming language, Bourne shell supports two loops, i.e., for loop and while loop. Let us look at an example to iterate over an array for a loop.

Code:

Arr = (1 2 3 4 5)
for i in "${Arr[@]}"
do
echo $i
done

Output:

Array in Unix eg9

Similarly, you can iterate over an array using a while loop as well.

Please keep in mind that various types of shells are available in Unix, like Bash, K shell, Bourne shell, etc. Shell is an interface to Unix systems with different commands and functions.So, different types of shells have associated syntax for arrays and their operations, with a slight variation in the command syntax mentioned earlier.

Bash Shell also permits the array operations on the variables without the variables being explicitly declared as an array. Let us look at the example to understand the above statement better.

Code:

String_variable = India is a democratic country
echo "${string[0]}"

Output:

array eg10

Conclusion

The array stores a collection of items in a contiguous memory location. The purpose of forming an array is to store multiple items of the same type with the same or different data types. In a practical scenario, why array structure lags popularity due to its lack of compatibility with various shell and complex structures.

Recommended Articles

We hope that this EDUCBA information on “Array in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Linux Proxy Server
  2. Types of Shells in Linux
  3. Create a User in Linux
  4. Unix File System
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW