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
- Introduction to Linux Shell and Shell Scripting
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Basic Operators in Shell Scripting
- How to check if a directory or a file exists in system or not using Shell Scripting?
- Create a password generator using shell scripting
- Leap Year Program with Bash Shell Scripting in Windows
- Basics of Batch Scripting
- Calculator Program with Batch Scripting in Windows
- How to find time taken by a command/program on Linux Shell?
- Making your own Linux Shell in C
- A Shell program To Find The GCD | Linux
- Reverse a String | Shell Programming
- Bash shell script to find sum of digits
- Developing a Linux based shell
- Bash shell script to swap two numbers
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
- String Operators | Shell Script
- Basic Shell Commands in Linux
- Implementing Directory Management using Shell Script
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.