Skip to main content
Shell Scripting – Complete Beginner to Advanced Guide
CHAPTER 10 Intermediate

Arrays and String Manipulation

Updated: May 16, 2026
25 min read

# CHAPTER 10

Arrays and String Manipulation

1. Introduction

Up to this point, our variables have acted as single-occupancy hotel rooms, holding only one piece of data at a time: SERVER="Web01". But what if you are writing a script to deploy security patches to a cluster of 50 web servers? Creating 50 different variables (SERVER1, SERVER2, SERVER3) is completely unmanageable. When a script requires the mass storage of grouped data, we utilize an Array. Furthermore, once that data is stored, we often need to manipulate the literal text strings—extracting prefixes, cutting off suffixes, or measuring length. In this chapter, we will evolve our scripts to handle bulk data. We will master indexed Arrays, learn to dynamically loop through massive datasets, and explore built-in string manipulation techniques to format our data precisely.

*(Important POSIX Note: Traditional Bourne Shell sh does NOT support true arrays. Arrays are a feature of bash, zsh, and ksh. Because arrays are mandatory for modern DevOps, this chapter will explicitly leverage Bash-compatible array syntax).*

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize an indexed array.
  • Retrieve specific elements from an array using zero-based index mapping.
  • Dynamically calculate the total length (number of items) of an array.
  • Iterate over an entire array systematically using a for loop.
  • Perform variable string manipulation (substring extraction and replacement).

3. Declaring and Accessing Arrays

An array is a single variable that holds a numbered list of items. Like almost all programming languages, arrays in shell scripting are "zero-indexed," meaning the first item is located at slot 0.

1. Declaring an Array: You define an array by wrapping a space-separated list of words in parentheses.

bash
123
#!/bin/bash
# Define an array of 4 distinct IP addresses
CLUSTER_IPS=("10.0.0.1" "10.0.0.2" "10.0.0.50" "10.0.0.99")

2. Accessing Elements: To pull a specific piece of data out of the array, you must explicitly use curly braces ${} and specify the slot number in square brackets [].

bash
12345
# Print the FIRST IP (Index 0)
echo "Primary DB IP is: ${CLUSTER_IPS[0]}"

# Print the THIRD IP (Index 2)
echo "Backup DB IP is: ${CLUSTER_IPS[2]}"

4. Advanced Array Operations

Arrays possess special wildcard symbols to help you extract metadata about the list.

1. Print All Elements (@): If you need to view every single item in the array simultaneously, use the @ (At) symbol as the index.

bash
1
echo "All nodes in the cluster: ${CLUSTER_IPS[@]}"

2. Get the Length of the Array (#): How many IPs are currently stored in the list? Prepend the Hash (#) symbol to the array name.

bash
12
echo "Total number of nodes active: ${#CLUSTER_IPS[@]}"
# Output: 4

3. Appending to an Array: To add a brand-new IP address to the end of the existing list without overwriting the old data, use the += operator.

bash
1
CLUSTER_IPS+=("10.0.0.100")

5. Iterating Over Arrays

The entire architectural purpose of an array is to feed its data into a for loop, allowing the computer to process the massive list automatically.
bash
123456789
#!/bin/bash
SERVERS=("Web01" "Web02" "DB01")

# The [@] expands the array into a list of words the loop can read
for TARGET in "${SERVERS[@]}"
do
    echo "Initiating deployment sequence on: $TARGET"
    # Execute actual deployment SSH commands here...
done

*Crucial Syntax:* Always wrap "${SERVERS[@]}" in double quotes. If one of the items in your array happens to contain a space, the double quotes prevent the for loop from accidentally splitting it into separate words.

6. String Manipulation

You don't always need complex external tools to modify text; the shell can do basic text formatting directly inside the variable expansion block.

1. String Length: Just like array length, use the Hash (#) to count the characters in a string.

bash
12
PASSWORD="SuperSecret123"
echo "Password length is: ${#PASSWORD} characters."

2. Substring Extraction: Extract a chunk of text. Syntax: ${VARIABLE:START_INDEX:LENGTH}

bash
1234
STRING="abcdefg"
# Start at index 2 (c), and grab 3 characters
echo "${STRING:2:3}" 
# Output: cde

3. String Replacement: Find and replace text directly inside the variable.

bash
1234
FILENAME="report_2026.txt"
# Replace .txt with .csv
NEW_FILE="${FILENAME/.txt/.csv}"
echo "Renamed to: $NEW_FILE"

7. Diagrams/Visual Suggestions

*Visual Concept: The Array Mailbox System* Draw a row of five connected mailboxes. Above the mailboxes, label the index numbers sequentially: [0], [1], [2], [3], [4]. Inside the mailboxes, place envelopes containing strings: "Red", "Blue", "Green". This visual instantly clarifies the conceptual difference between the variable name (MAILBOX), the Index pointer ([1]), and the actual Data payload ("Blue").

8. Best Practices

  • Use Arrays for Complex Command Flags: If your script needs to execute a complex command that requires 10 different flags, do not write them as one massive, unreadable string. Build an array of flags (FLAGS=("-v" "--force" "--secure")), and then pass the expanded array into the command (my_command "${FLAGS[@]}"). This makes your code infinitely easier to read, maintain, and modify.

9. Common Mistakes

  • Forgetting the Curly Braces: When beginners attempt to print the first item of an array, they often type echo $SERVERS[0]. The shell evaluates $SERVERS (which defaults to the first item), prints it, and then literally prints the text "[0]" immediately after it. You MUST isolate the entire array call within curly braces: echo ${SERVERS[0]}.

10. Mini Project: Build a Simple Contact Manager

Let's build a script that stores an array of employee names and utilizes a loop to generate email addresses through string manipulation.
  1. 1. nano contacts.sh
  1. 2. Write the code:
bash
12345678910111213141516171819
#!/bin/bash

# Define an array of employees
EMPLOYEES=("Alice Smith" "Bob Johnson" "Charlie Davis")

echo "Generating Corporate Email Directory..."
echo "-------------------------------------"

# Loop through the array
for PERSON in "${EMPLOYEES[@]}"
do
    # 1. Convert the entire name to lowercase (Bash 4.0+ feature)
    LOWER_NAME="${PERSON,,}"
    
    # 2. Replace the space with a dot to format the email
    EMAIL="${LOWER_NAME/ /.}@corporate.com"
    
    echo "Employee: $PERSON | Email: $EMAIL"
done
  1. 3. Run it. You have dynamically processed and formatted bulk text data utilizing arrays and built-in string replacement.

11. Practice Exercises

  1. 1. Given the array FILES=("config.ini" "data.csv" "script.sh"), write the exact shell syntax required to mathematically determine the total number of items stored inside the array.
  1. 2. Provide the syntax to extract exactly the first 4 characters from a variable named PRODUCTCODE.

12. MCQs with Answers

Question 1

A script declares the array DEPARTMENTS=("HR" "Finance" "Engineering" "Marketing"). What is the correct syntax to output the exact word "Engineering" to the terminal?

Q2. When engineering a for loop to iterate through every single element of a predefined array named USER
LIST, what specific wildcard symbol is placed inside the array brackets to instruct the shell to expand the entire list simultaneously? a) * b) # c) @ d) % Answer: c) @ *(Example: "${USER_LIST[@]}")*

13. Interview Questions

  • Q: A junior engineer writes a script containing the variable call echo $SERVERS[1]. The terminal outputs Web01[1] instead of the actual second server in the array. Explain the mechanical syntax error the engineer made regarding variable expansion evaluation.
  • Q: Explain the precise structural difference between utilizing "${ARRAY[*]}" and "${ARRAY[@]}" within a script, particularly focusing on how double quotes interact with the expansion regarding word splitting.
  • Q: You are tasked with writing a script that receives a massive, comma-separated string (DATA="ip,hostname,mac,status"). While you could use external tools, describe how you might utilize built-in variable string manipulation to replace those commas with spaces directly in the shell.

14. FAQs

Q: Can I have an array inside another array? A: No, standard Shell scripting (even Bash) does not natively support multi-dimensional arrays (like matrices). If you require complex, nested data structures, it is highly recommended to transition your automation script to Python.

15. Summary

In Chapter 10, we dramatically scaled the data-handling capacity of our automation scripts. We transitioned from single-string variables to the bulk storage architecture of Arrays, mastering zero-based indexing to extract precise data points. We utilized the @ symbol to expand lists, seamlessly integrating arrays with for loops to generate highly scalable, repetitive execution engines. Finally, we bypassed the need for heavy external text utilities by leveraging built-in String Manipulation syntax, allowing us to rapidly slice substrings, measure character lengths, and execute dynamic find-and-replace text formatting directly within our script's memory.

16. Next Chapter Recommendation

Your script can handle basic text manipulation in memory. But what if you need to extract specific rows and columns of text from a massive 5-Gigabyte log file? Proceed to Chapter 11: Text Processing Utilities.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·