Arrays in unix shell?

Arunachalam picture Arunachalam · Dec 10, 2009 · Viewed 262.7k times · Source

How do I create an array in unix shell scripting?

Answer

Vincent Agami picture Vincent Agami · Sep 19, 2013

The following code creates and prints an array of strings in shell:

#!/bin/bash
array=("A" "B" "ElementC" "ElementE")
for element in "${array[@]}"
do
    echo "$element"
done

echo
echo "Number of elements: ${#array[@]}"
echo
echo "${array[@]}"

Result:

A
B
ElementC
ElementE

Number of elements: 4

A B ElementC ElementE