Shell Script Dizi: Bash’te Arrays ile Veri Yönetimi Rehberi
If you are working with a lot of data in shell scripts, dealing with individual variables becomes tiring.
In this guide, you will learn how to bulk manage data with arrays.
What Will You Learn in This Guide?
- Difference of arrays from variables
- Indexed and associative array types
- Element adding and reading methods
- Navigating the series with loops
- Basic operations such as counting and deleting
What is Array?
Variables store a single value.
Arrays hold multiple values under the same name.
Arrays are more practical for large lists.
It saves lives in tasks such as log, server list, file list.
1. Array Types in Shell Script
There are two main types of arrays in shell scripts:
- Indexed Arrays
Elements are kept with numbers starting from 0. - Associative Arrays
Elements are stored as key-value.
2. Array Definition (declare)
declare -a indisli_dizi
declare -A iliskisel_dizi
- These commands clearly define the array.
- Capital A is for relational.
- Small a is for subscript.
3. Adding and Reading Array Elements
Adding element to associative array
iliskisel_dizi[anahtar1]="Merhaba Dünya"
echo ${iliskisel_dizi[anahtar1]}
- This command adds and prints the value by key.
Creating an indexed array
sayi_dizisi=(10 20 30 40 50)
echo ${sayi_dizisi[0]}
- This command creates the array and prints the first element.
4. Reading Array with a Loop
#!/bin/bash
sunucular=(tr1-node01 tr1-node02 tr1-node03)
for sunucu in ${sunucular[@]}
do
echo "Sunucu Adı: $sunucu"
done
- This script processes array elements sequentially.
Hint: The attached servers tag is safer for values with spaces.
5. Built-in Array Operations
- Getting all elements
echo ${sunucular[@]}
- This command prints all elements in the array.
- Finding the number of elements
echo ${#sunucular[@]}
- This command shows how many elements are in the array.
- Deleting a single element
unset sunucular[1]
- This command deletes the element at the specified index.
- Completely clear the array
unset sunucular
- This command removes the entire array.
Frequently Asked Questions (FAQ)
1. Are numbers and text kept at the same time in shell arrays? Yes. Numbers and text can be mixed in the same string.
2. What is the difference between @ and *? They both represent all elements. The "array" tag in double quotes is safer.
3. Why do we use [] in the loop? Because it allows you to cycle through all the elements one by one. This usage is the most common method.
4. When does an associative array make sense? It makes sense in pairings such as setting, label, name-value. Example: user_role[]="admin" etc.
Result
Shell scripts accelerate automation. It allows you to manage data in a more organized and readable manner.
You can try it now on the GenixNode infrastructure to run your scripts on high-performance Linux servers.

