Consider a Situation if we want to store 1000 numbers and perform operations on them. If we use simple variable concept then we have to create 1000 variables and the perform operations on them. But it is difficult to handle a large number of variables. So it is good to store the same type of values in the array and then access via index number.
Array in Shell Scripting
An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable which contains multiple values may be of same type or different type since by default in shell script everything is treated as a string. An array is zero-based ie indexing start with 0.
How to Declare Array in Shell Scripting?
We can declare an array in a shell script in different ways.
1. Indirect Declaration
In Indirect declaration, We assigned a value in a particular index of Array Variable. No need to first declare.
2. Explicit Declaration
In Explicit Declaration, First We declare array then assigned the values.
declare -a ARRAYNAME
3. Compound Assignment
In Compount Assignment, We declare array with a bunch of values. We can add other values later too.
ARRAYNAME=(value1 value2 .... valueN)
ARRAYNAME=(=10 =20 =30)
To Print Array Value in Shell Script?
To Print All elements
[*] means All elements of Array.
prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav
To Print first element
To Print Selected index element
To print elements from a particular index
prakhar ankit 1 rishabh manish abhinav ankit 1 rishabh manish abhinav 1 rishabh manish abhinav prakhar
To print elements in range
ankit 1 rishabh manish 1 rishabh manish rak
To count Length of in Array
To count the length of a particular element in Array.
Use #(hash) to print length of particular element
To count length of Array.
To Search in Array
arr[@] : All Array Elements.
/Search_using_Regular_Expression/ : Search in Array
Search Returns 1 if it found the pattern else it return zero. It does not alter the original array elements.
To Search & Replace in Array
//Search_using_Regular_Expression/Replace : Search & Replace
Search & Replace does not change in Original Value of Array Element. It just returned the new value. So you can store this value in same or different variable.
prAkhAr Ankit 1 rishAbh mAnish AbhinAv prakhar ankit 1 rishabh manish abhinav RakhaR
To delete Array Variable in Shell Script?
To delete index-1 element
To delete the whole Array
prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav prakhar ankit 1 rishabh manish abhinav prakhar prakhar rishabh ankit prakhar ankit 1 rishabh manish abhinav ankit 1 rishabh manish abhinav 1 rishabh manish abhinav rakhar ankit 1 rishabh manish 1 rishabh manish rak 7 7 6 6 1 prAkhAr Ankit 1 rishAbh mAnish AbhinAv prakhar ankit 1 rishabh manish abhinav pRakhaR
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Introduction to Linux Shell and Shell Scripting
- Basic Operators in Shell Scripting
- Create a password generator using shell scripting
- How to check if a directory or a file exists in system or not using Shell Scripting?
- Making your own Linux Shell in C
- Conditional Statements | Shell Script
- Developing a Linux based shell
- String Operators | Shell Script
- Basic Shell Commands in Linux
- Reverse a String | Shell Programming
- Looping Statements | Shell Script
- A Shell program To Find The GCD | Linux
- Implementing Directory Management using Shell Script
- Bash shell script to find sum of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.