Arrays and String Manipulation
# 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
forloop.
- 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.
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 [].
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.
2. Get the Length of the Array (#):
How many IPs are currently stored in the list? Prepend the Hash (#) symbol to the array name.
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.
5. Iterating Over Arrays
The entire architectural purpose of an array is to feed its data into afor loop, allowing the computer to process the massive list automatically.
*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.
2. Substring Extraction:
Extract a chunk of text. Syntax: ${VARIABLE:START_INDEX:LENGTH}
3. String Replacement: Find and replace text directly inside the variable.
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.
nano contacts.sh
- 2. Write the code:
- 3. Run it. You have dynamically processed and formatted bulk text data utilizing arrays and built-in string replacement.
11. Practice Exercises
-
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.
-
2.
Provide the syntax to extract exactly the first 4 characters from a variable named
PRODUCTCODE.
12. MCQs with Answers
A script declares the array DEPARTMENTS=("HR" "Finance" "Engineering" "Marketing"). What is the correct syntax to output the exact word "Engineering" to the terminal?
for loop to iterate through every single element of a predefined array named USERLIST, 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 outputsWeb01[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.