# Les boucles



### For

```bash
array=('item1' 'item2' 'item3')


for i in "${array[@]}"
do
   echo "$i"
done
```

```bash
fruits=('apple' 'banana' 'cherry')
colors=('red' 'yellow' 'red')

for i in "${!fruits[@]}"
do
    echo "The ${fruits[$i]} is ${colors[$i]}"
done
```

```bash
numbers=(1 2 3 4 5)
for num in "${numbers[@]}"
do
    if ((num % 2 == 0))
    then
        echo "$num is even"
    else
        echo "$num is odd"
    fi
done
```

```bash
declare -A fruits
fruits=( [apple]='red' [banana]='yellow' [cherry]='red' )

for fruit in "${!fruits[@]}"
do
    echo "The $fruit is ${fruits[$fruit]}"
done
```



### While

```bash
fruits=('apple' 'banana' 'cherry')
index=0

while [ $index -lt ${#fruits[@]} ]
do
    echo "I like ${fruits[$index]}"
    ((index++))
done
```



### Until

```bash
numbers=(1 2 3 4 5)
index=0

until [ $index -eq ${#numbers[@]} ]
do
    echo "Processing number: ${numbers[$index]}"
    ((index++))
done
```



### mapfile

```bash
mapfile -t lines < file.txt
for line in "${lines[@]}"
do
    echo "Processing line: $line"
done
```

