Tsallaka zuwa abun ciki
Justice Tech School

Indexed Arrays

Arrays hold many values. Use arr=(a b c), ${arr[0]}, ${arr[@]} for all, ${#arr[@]} for the count, += to append, unset to remove and ${arr[@]:1:2} to slice.

Example
arr=(red green blue)
arr+=(yellow)
echo "${arr[1]} ${#arr[@]}"
echo "${arr[@]:1:2}"
unset "arr[0]"
echo "${arr[@]}"
for c in "${arr[@]}"; do echo -n "[$c]"; done; echo

Result

green 4
green blue
green blue yellow
[green][blue][yellow]

Exercise

Count the items.

echo ${arr[@]}

Quiz Test

How do you get all elements?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.