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

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

2 weeks ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

2 weeks ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

4 weeks ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

4 weeks ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

4 weeks ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

4 weeks ago