Array in Bash Scripting | Bash Array Practicals with Example

array-in-bash-scripting-introduction
  • Save

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.

Table of Contents

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

  1. In an Array, we can store the multiple elements of different data types.
  2. We can add or retrieve the array element based on the index number.
  3. It solves the problem of creating a new variable for each value.

Disadvantages

  1. Bash Scripting does not provide support for multi-dimensional arrays.
  2. 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]}"

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

What is an array in bash?

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.

How do you declare an array in bash?

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

How to read 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]}”

How to print an array in bash?

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

  1. Directory in Linux Define | Linux Directory & its Commands
  2. Explain the chmod command in Linux | Linux chmod command
  3. Linux User Management || User Management in Linux
  4. Linux Computer Network Advanced Command | Network Command
  5. Redirection in Linux I/O| Linux I/O Redirection
  6. CronTab and Job Scheduling in Linux | Make CronTab Project
  7. Linux Firewall Unlock Rules with Firewall-cmd Tutorial
  8. netstat command in Linux | Linux netstat command
  9. SSH Command Full Guide with Practical | Linux SSH Service
  10. awk command Guide | How to arrange the output of the file in Linux
  11. sed command Full Guide Tutorial | Linux sed Command
  12. Iptables commands Full Guide: How to make our own Firewall

Recent Articles on Computer Networks

  1. Introduction to Computer Networking | What is Computer Network
  2. What are Topology & Types of Topology in Computer Network
  3. What is FootPrinting in Cyber Security and its Types, Purpose
  4. Introduction to Cloud Computing | What is Cloud Computing
  5. Distributed Shared Memory and Its Advantages and Disadvantages
  6. What is a VPN? How does a VPN Work? What VPN should I use?
  7. What is an Internet and How the Internet Works
  8. What is a Website and How Does a Website or web work?
  9. Introduction to Virus and Different Types of Viruses in Computer
  10. What is TCP and its Types and What is TCP three-way Handshake
  11. What is the UDP Protocol? How does it work and what are its advantages?
  12. What is an IP and its Functions, What is IPv4 and IPv6 Address
  13. What is MAC Address and its Types and Difference MAC vs IP
  14. What is ARP and its Types? How Does it Work and ARP Format
  15. Sessions and Cookies and the Difference Between Them
  16. What is ICMP Protocol and its Message Format?
  17. What is Big Data? Characteristics and Types of Big Data
  18. Disciplines of CyberSecurity | What are the goals of CyberSecurity?
  19. What is Firewall, Features, Types and How does the Firewall Work?
  20. Network Scanning, Types, and Stealth Scan in Computer Network
  21. Cryptography and its Types in Ethical Hacking
  22. Tor Browser and How Does It Work | Onion Router Tutorial
  23. Proxy Server, Advantages, Difference between Proxy Server & VPN
  24. DHCP Protocol and What Are the Pros and Cons of DHCP
  25. Intrusion Detection System(IDS) and What are the types of IDS
  26. Domain Name Server, How Does It Work, and its advantages
  27. Telnet: Introduction, How Does it Work, and Its Pros and Cons
  28. SOC: Introduction, Functions performed by SOC, and its Pros
  29. What is SIEM? | What is the Difference between SIEM and SOC?
  30. Application Layer in OSI Model | OSI Model Application Layer
  31. What is SSL Protocol or SSL/TLS and SSL Handshake, and Architecture of SSL
  32. What are Servers, how do they work, and its different Types
  33. Network Devices-Router, Switch, Hub, etc in Computer Network
  34. Connection Oriented and Connection-less Services in Network
  35. Physical Layer in OSI Model | OSI Model Physical Layer
  36. Presentation Layer in OSI Model | OSI Model Presentation Layer
  37. Session layer in OSI Model | OSI Model Session layer
  38. Transport Layer in OSI Model | Computer Network Transport Layer
  39. Network Layer in OSI Model | OSI Model Network Layer
  40. Data Link Layer in OSI Model | OSI Model Data Link Layer
  41. Block Diagram of Communication System with Detailed Explanation
Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
0 Shares
Share via
Copy link