EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial Array in Unix
Secondary Sidebar
Unix Tutorial
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Array in Unix

By Priya PedamkarPriya Pedamkar

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

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

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

1. We will create the 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 that is passed as an argument to the command. The syntax of ‘echo’ is mentioned below.

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: { } curly braces are used to refer to the content of the array.

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: # is used to find the length of a specific element of an array.

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

Code:

echo ${#first_name[0]}

Output:

eg5

7. To get the index of the array to use the below command.

Code:

echo ${!first_name[@]}

Output:

eg6

Note: ! is used to get the index of the array.

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:

Array in Unix eg7

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:

Array in Unix eg8

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

Array in Unix eg9

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:

Array in Unix eg9

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:

array eg10

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 –

  1. Linux Proxy Server
  2. Types of Shells in Linux
  3. Create User in Linux
  4. Unix File System
Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)
  16 Online Courses |  3 Hands-on Projects |  160+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Kali Linux Training (3 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

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

EDUCBA Login

Forgot Password?

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

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

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

EDUCBA

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

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

Let’s Get Started

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