Array in Bash Scripting | Bash Array Practicals with Example
Are you frustrated with the result of the array in bash scripting? So don’t worry then you have come to the right place. Because in this blog I will tell you about arrays in Bash scripting, what are the functions we can use in arrays, and many such questions related to an array. So let’s get started with the blog.
Introduction
An array is one of the important concepts in Bash Scripting or Programming. Using arrays, we may store and retrieve elements in list form, which is useful for some applications. Additionally, arrays in bash enable us to write command-line programs for storing data in a list manner. Array in bash does not discriminate the string from a number, so an array can contain both strings and numbers.
Advantages
- In an Array, we can store the multiple elements of different data types.
- We can add or retrieve the array element based on the index number.
- It solves the problem of creating a new variable for each value.
Disadvantages
- Bash Scripting does not provide support for multi-dimensional arrays.
- In another scripting or programming language, an array can contain another array but in bash, an array can’t store another array.
Declaration of an Array in Bash Scripting
Any variable can be used as an indexed array without having to declare it.
Use the keyword “declare” and the following syntax to explicitly declare a variable as a Bash Array:
declare -a array_name
Here,
array_name indicates the name of the array
In a Simple way, we can create an indexed array in the following form
array_name[index_num1]=value1
array_name[index_num2]=value2
array_name[index_num3]=value3
array_name[index_num4]=value4
Here,
index_num defined the positive index position of an array.
The index number starts from 0.
#!/bin/bash
## Creating an Indexed Array
declare -a emp_name
emp_name[0]="Alex"
emp_name[1]="Sam"
emp_name[2]="Roger"
emp_name[3]="Travis"
echo "${emp_name[@]}"
1) Creating Associative Arrays
The associative arrays are first declared, unlike numerically indexed arrays.
The associative arrays can be declared by using the keyword “declare” and the -A (uppercase) option. The syntax is as follows:
declare -a array_name
The method to create an associative array can be defined as:
declare -a array_name=(
array_name[index_num1]=value1
array_name[index_num2]=value2
array_name[index_num3]=value3
array_name[index_num4]=value4
)
For Example:
#!/bin/bash
## Creating an Indexed Array
declare -a sports=(
[0]=baseball
[1]=cricket
[2]=football
[3]=basketball
)
echo "${sports[@]}"
2) Initialization of an Array in Bash
To initialize an array in Bash, we can use the assignment operator(=), simply putting a list of the elements inside parenthesis and separating them with spaces.
#!/bin/bash
## Scripting the index of an array
declare -a company_name=("Welcome" "to" "BinFin Tech")
echo "${!company_name[@]}"
Accessing a Single Element from the Array
To access a single element from an array, use the index number inside the square brackets.
echo ${array_name[index]}
Example:
#!/bin/bash
## Scripting the index of an array
declare -a company_name=("Welcome" "to" "BinFin Tech")
echo "${company_name[2]}"
Print the Bash Array
To print all the elements of a Bash Array together with their indices and details, we can use the keyword “declare” and the “-p” option. The following can be used to print the Bash Array:
#!/bin/bash
## Creating an Indexed Array
declare -a sports=(
[0]=baseball
[1]=cricket
[2]=football
[3]=basketball
)
declare -p sports
Operations performed on Array in Bash Scripting
A) Printing the keys of an Array
#!/bin/bash
## Scripting the index of an array
declare -a company_name=("Welcome" "to" "BinFin Tech")
echo "${!company_name[@]}"
echo "${company_name[2]}"
B) Printing the length of an Array
#!/bin/bash
## Length of the array
declare -a company_name=("Welcome" "to" "Bin" "Fin" "Tech")
echo "The length of the array is:" ${#company_name[@]}
echo "Array contain ${#company_name[@]} elements"
C) Loop through the Array
#!/bin/bash
company_name=(Welcome to BinFin Tech)
for i in "${company_name[@]}"
do
echo "$i"
done
D) Adding Elements to an Array
#!/bin/bash
## Adding elements to an array
declare -a employee_name=("Alex" "Sam" "Roy" "Travis")
echo ${employee_name[@]}
employee_name[4]="Cooper"
echo "Adding an element in an array"
echo "${employee_name[@]}"
## Adding multiple elements in an array
employee_name+=("Maverick" "Dominic" "Flash")
echo "${employee_name[@]}"
E) Updating the element of an Array
#!/bin/bash
## Updating the element of an array
declare -a employee_name=("Alex" "Copper" "Sam")
echo ${employee_name[@]}
echo "After updating the array"
employee_name[2]="Sammy"
echo ${employee_name[@]}
F) Deleting an element from an Array
#!/bin/bash
## Delete an element in the array
declare -a employee_name=("Alex" "Sam" "Copper" "Travis")
echo ${employee_name[@]}
echo "After deleting an element in an array"
unset employee_name[1]
echo ${employee_name[@]}
echo ${!employee_name[@]}
G) Deleting the Entire Array
#!/bin/bash
## Delete an element in the array
## Deleting the entire array
declare -a employee_name=("Alex" "Sam" "Copper" "Travis")
echo "Before deleting an entire array"
echo ${#employee_name[@]}
unset employee_name
echo "After deleting an entire array"
echo ${#employee_name[@]}
H) Slicing the element of an Array
#!/bin/bash
## Slicing the array from index 1 to 5
declare -a employee=("Alex" "Sam" "Travis" "Copper" "Dominic" "Maverick")
for e in "${employee}"
do
echo "${employee[@]:1:5}"
done
Note: This blog is mainly referenced from the Javatpoint.
FAQ
An array is one of the important concepts in Bash Scripting or Programming. Using arrays, we may store and retrieve elements in list form, which is useful for some applications. Additionally, arrays in bash enable us to write command-line programs for storing data in a list manner. Array in bash does not discriminate the string from a number, so an array can contain both strings and numbers.
The array can be declared in the following ways
1) Creating Numerically Indexed Arrays
2) Creating Associative Arrays
3) Initialization of an Array in Bash
Just pass the index number inside the square brackets.
!/bin/bash
Scripting the index of an array
declare -a company_name=(“Welcome” “to” “BinFin Tech”)
echo “${company_name[2]}”
To print all the elements of a Bash Array together with their indices and details, we can use the keyword “declare” and the “-p” option
declare -p array_name
!/bin/bash
Creating an Indexed Array
declare -a sports=(
[0]=baseball
[1]=cricket
[2]=football
[3]=basketball
)
declare -p sports
Recent Articles on Linux Operating System
- Directory in Linux Define | Linux Directory & its Commands
- Explain the chmod command in Linux | Linux chmod command
- Linux User Management || User Management in Linux
- Linux Computer Network Advanced Command | Network Command
- Redirection in Linux I/O| Linux I/O Redirection
- CronTab and Job Scheduling in Linux | Make CronTab Project
- Linux Firewall Unlock Rules with Firewall-cmd Tutorial
- netstat command in Linux | Linux netstat command
- SSH Command Full Guide with Practical | Linux SSH Service
- awk command Guide | How to arrange the output of the file in Linux
- sed command Full Guide Tutorial | Linux sed Command
- Iptables commands Full Guide: How to make our own Firewall
Recent Articles on Computer Networks
- Introduction to Computer Networking | What is Computer Network
- What are Topology & Types of Topology in Computer Network
- What is FootPrinting in Cyber Security and its Types, Purpose
- Introduction to Cloud Computing | What is Cloud Computing
- Distributed Shared Memory and Its Advantages and Disadvantages
- What is a VPN? How does a VPN Work? What VPN should I use?
- What is an Internet and How the Internet Works
- What is a Website and How Does a Website or web work?
- Introduction to Virus and Different Types of Viruses in Computer
- What is TCP and its Types and What is TCP three-way Handshake
- What is the UDP Protocol? How does it work and what are its advantages?
- What is an IP and its Functions, What is IPv4 and IPv6 Address
- What is MAC Address and its Types and Difference MAC vs IP
- What is ARP and its Types? How Does it Work and ARP Format
- Sessions and Cookies and the Difference Between Them
- What is ICMP Protocol and its Message Format?
- What is Big Data? Characteristics and Types of Big Data
- Disciplines of CyberSecurity | What are the goals of CyberSecurity?
- What is Firewall, Features, Types and How does the Firewall Work?
- Network Scanning, Types, and Stealth Scan in Computer Network
- Cryptography and its Types in Ethical Hacking
- Tor Browser and How Does It Work | Onion Router Tutorial
- Proxy Server, Advantages, Difference between Proxy Server & VPN
- DHCP Protocol and What Are the Pros and Cons of DHCP
- Intrusion Detection System(IDS) and What are the types of IDS
- Domain Name Server, How Does It Work, and its advantages
- Telnet: Introduction, How Does it Work, and Its Pros and Cons
- SOC: Introduction, Functions performed by SOC, and its Pros
- What is SIEM? | What is the Difference between SIEM and SOC?
- Application Layer in OSI Model | OSI Model Application Layer
- What is SSL Protocol or SSL/TLS and SSL Handshake, and Architecture of SSL
- What are Servers, how do they work, and its different Types
- Network Devices-Router, Switch, Hub, etc in Computer Network
- Connection Oriented and Connection-less Services in Network
- Physical Layer in OSI Model | OSI Model Physical Layer
- Presentation Layer in OSI Model | OSI Model Presentation Layer
- Session layer in OSI Model | OSI Model Session layer
- Transport Layer in OSI Model | Computer Network Transport Layer
- Network Layer in OSI Model | OSI Model Network Layer
- Data Link Layer in OSI Model | OSI Model Data Link Layer
- Block Diagram of Communication System with Detailed Explanation
0