Bash Arrays, Strings, and Text Handling/Loop through key/value pairs

Iterate through an associative array.

Section: Associative Arrays

Loop through key/value pairs

bash
bash
for k in "${!ports[@]}"; do echo "$k=${ports[$k]}"; done
Explanation

Order is not guaranteed.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Associative Arrays
Create associative array
Declare an associative array.
OpenIn sheetbashsame section
Read associative array item
Read a value by key.
OpenIn sheetbashsame section
List associative array keys
Expand associative array keys.
OpenIn sheetbashsame section
Loop through array
Iterate through array elements safely.
OpenIn sheetbash2 tag match
Create indexed array
Create an indexed array.
OpenIn sheetbash1 tag match
Access array element
Read an array element by index.
OpenIn sheetbash1 tag match