Creating an array
You create an indexed array with parentheses on the right side of an assignment. Each word inside the parentheses becomes one element. Quoting still applies, so a value with a space must be quoted to remain one element. The elements are numbered from zero, and the array can be sparse, which means some indices can be missing.
fruits=(apple "red delicious" cherry)
echo "${fruits[1]}"The second line prints red delicious, which is a single element. Reading an element always needs the braces, because $fruits[1] means the scalar $fruits followed by the text [1]. The expansion ${fruits[1]} is the form to use.
Expanding all elements
The two expansions "${fruits[@]}" and "${fruits[*]}" look similar but behave differently. The quoted @ form expands to one word per element, keeping each element intact even if it contains spaces. The quoted * form joins all elements into a single word, separated by the first character of IFS. In almost every script you want the @ form.
fruits=(apple "red delicious" cherry)
printf "[%s]\n" "${fruits[@]}"
printf "[%s]\n" "${fruits[*]}"The first printf prints three bracketed lines, one for each element, including [red delicious]. The second prints one line, [apple red delicious cherry], because the star form joined everything into a single argument. Forgetting the quotes or using the star form is the most common array bug.
Length, indices, and appending
${#fruits[@]} gives the number of elements, and ${#fruits[1]} gives the length of one element. The expansion ${!fruits[@]} lists the indices that are set, which matters for sparse arrays. You can add an element to the end with +=, and you can set any index directly.
fruits=(apple "red delicious" cherry)
fruits+=(mango)
fruits[10]=kiwi
echo "count=${#fruits[@]}"
echo "indices=${!fruits[@]}"The output is count=5 and indices=0 1 2 3 10. The array holds five elements, but index 10 is the highest, and indices 4 to 9 are not set. A loop written as for ((i=0; i<${#fruits[@]}; i++)) would miss kiwi and would try to read unset elements. Loop over the indices or over the values instead.
Indexed arrays in bash start at zero, and negative indices count from the end. ${fruits[-1]} is the last element, which was added in bash 4.3. Older shells do not support it, so use it only where you control the bash version.
Iterating over an array safely
The standard way to loop over an array is for item in "${fruits[@]}". The quotes keep each element together, and the loop sees exactly the elements that were stored. To loop with indices, use for i in "${!fruits[@]}" and read ${fruits[$i]} inside the body. This form still works after you remove an element with unset, which leaves a hole in the index sequence.
fruits=(apple "red delicious" cherry)
unset "fruits[1]"
for i in "${!fruits[@]}"; do echo "$i: ${fruits[$i]}"; doneThe loop prints 0: apple and 2: cherry. The index 1 is missing, and the loop skips it without a special case. Notice that the subscript in unset is quoted, so the brackets are not treated as a glob.
Arrays from command output
Arrays are a good way to collect the lines produced by a command. Bash 4 and later provide mapfile, also called readarray, which reads standard input into an array with one element per line. The -t option removes the trailing newline from each element.
mapfile -t lines < <(printf "%s\n" alpha beta gamma)
echo "${#lines[@]} lines, second is ${lines[1]}"The output is 3 lines, second is beta. The process substitution <(...) feeds the output of the command to mapfile as if it were a file, and it runs in the current shell, so the array is still set afterward.
Hands-on: build and inspect an array
Run the block below and compare the output with your expectation before you run it. Then change the quotes in the last line and see how the output shifts. The lab for this unit asks you to write a script that does the same thing and leaves its output in a file.
tools=(ls "grep -r" awk)
echo "count=${#tools[@]}"
printf "<[%s]>\n" "${tools[@]}"The output shows three elements, and the element grep -r stays one element, because the quoted expansion keeps it together.