FreeKB - Bash Shell Scripting Append values to an array (+=)
Bash Shell Scripting - Append values to an array (+=)

Let's say you have an array of fruit.

fruit=(banana apple orange grapes)

 

The += operator can be used to append values to the array. In this example, pineapple is appended to the fruit array.

fruit+=(pineapple)

 

The array can be printed.

echo $fruit

 

Echoing the array will show that pineapple has been appended to the array.

banana apple orange grapes pineapple

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter fe471 in the box below so that we can be sure you are a human.




Comments


July 24th, 2020 by Robin A. Meade
echo "$fruit" # prints the first element of the array printf '%s\n' "${fruit[@]}" # prints each element on its own line IFS=',' echo "${fruit[*]}" # prints comma separated values IFS=$' \t\n' # Restores IFS to its default value

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |