Arrays in Bash empower you to store multiple values in an indexed collection, offering direct access to any element—first, middle, or last—by its index. This is analogous to reaching into a specific drawer when you know exactly where your socks are kept, rather than inspecting each one sequentially.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

Bash arrays are zero-indexed: the first element is at index
0, the second at 1, and so on.Why Use Arrays?
- Direct element access reduces loops and conditional checks.
- Cleaner scripts when handling lists of servers, filenames, or configurations.
- Improved performance versus string-based lists requiring manual parsing.
Declaring Arrays
1. Assigning by Index
![The image illustrates an array with three indexed elements (0, 1, 2), highlighting the second element (index 1) as "server[1]".](https://kodekloud.com/kk-media/image/upload/v1752868542/notes-assets/images/Advanced-Bash-Scripting-Arrays/array-indexed-elements-server1.jpg)
2. Inline Initialization
declare -a:
Accessing Array Elements
| Operation | Syntax | Example Output |
|---|---|---|
| Single element | ${array[index]} | ${course_sections[1]} → Coding Standards |
| All elements | ${array[@]} | ${course_sections[@]} → all items |
| Number of elements | ${#array[@]} | count of items |
| All indices (keys) | ${!array[@]} | list of valid indices |
| Slice | ${array[@]:start:length} | subset of elements |
Get All Elements
Quoting Arrays: Preserve Whitespace
Unquoted expansions split on spaces:Always quote
Unquoted expansions can lead to unexpected word splitting.
${array[@]} in loops or commands to preserve elements containing spaces.Unquoted expansions can lead to unexpected word splitting.
Modifying Arrays
Append an Element
Overwrite Entire Array
Assigning a string replaces index0 but retains the array type: