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.
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 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]}.
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
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.
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
You can add a new item to an existing array using +=.
#!/bin/bashtools=(nmap curl)tools+=(nikto)echo "${tools[@]}" Output:
nmap curl nikto
#!/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.
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.
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…
Introduction Working with files and directories is one of the most important skills in Bash…