How To

How To Use Arrays In Bash Scripts

Introduction

Arrays are an important part of Bash scripting. A Bash array allows you to store multiple values inside a single variable. Instead of creating separate variables for each value, you can store related items together and access them easily.

Arrays are useful when working with lists of usernames, files, directories, IP addresses, tools, services, or commands. If you are learning Bash scripting, arrays will help you write cleaner and more organized scripts for Linux automation, system administration, cybersecurity tasks, and log processing.

What Is A Bash Array?

A Bash array is a variable that can hold more than one value.

Basic syntax:

array_name=(value1 value2 value3)

Example:

tools=(nmap curl grep awk)

Here, tools is an array containing four values.

Create A Bash Array

Create a new Bash script:

nano array-example.sh

Add the following code:

#!/bin/bashtools=(nmap curl grep awk)echo "First tool: ${tools[0]}"echo "Second tool: ${tools[1]}"echo "Third tool: ${tools[2]}"

Save and run the script:

chmod +x array-example.sh./array-example.sh

Output:

First tool: nmapSecond tool: curlThird tool: grep

In Bash arrays, indexing starts from 0. That means the first item is accessed using ${array[0]}.

Print All Array Values

To print all values from an array, use:

echo "${tools[@]}"

Example:

#!/bin/bashtools=(nmap curl grep awk)echo "Available tools:"echo "${tools[@]}"

Output:

Available tools:nmap curl grep awk

Loop Through A Bash Array

Arrays are commonly used with loops.

#!/bin/bashtools=(nmap curl grep awk)for tool in "${tools[@]}"do    echo "Checking tool: $tool"done

This script loops through each item in the array and prints it.

Get Array Length

To find the number of items in an array, use:

${#array_name[@]}

Example:

#!/bin/bashtools=(nmap curl grep awk)echo "Total tools: ${#tools[@]}"

Output:

Total tools: 4

Add New Item To Array

You can add a new item to an existing array using +=.

#!/bin/bashtools=(nmap curl)tools+=(nikto)echo "${tools[@]}"

Output:

nmap curl nikto

Cybersecurity Example: Check Installed Tools

#!/bin/bashtools=(nmap curl git nikto)for tool in "${tools[@]}"do    if command -v "$tool" > /dev/null 2>&1; then        echo "$tool is installed"    else        echo "$tool is not installed"    fidone

This script checks whether common cybersecurity tools are installed on the Linux system.

Conclusion

Bash arrays are useful for storing and managing multiple values in one variable. You can create arrays, access items by index, print all values, loop through items, count array elements, and add new values.

For beginners, learning Bash arrays is important because many real-world scripts work with lists. Arrays are helpful in automation, file handling, cybersecurity scripting, tool checking, and Linux administration tasks.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

3 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

Docker is an open-source platform that lets you package and run applications inside containers. Each container…

13 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

14 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

14 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

14 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

16 hours ago