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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

4 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

4 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

4 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

4 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

4 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

5 days ago