How to use bash array?

bash array shell multiline line

Bash shell has syntax for arrays, values should be separated by space and wrapped with round brackets:59a94420fa767fcd0e133132

Defining array in bash:

dirs=('etc' 'www')

Often arrays tend to be large, so multiline definitions are preferred. It is possible in bash to define array spanning multiple lines by just splitting lines between elements:


As whitespace is ignored in array definition, it can be nicely formatted so that each element can be easily commented:


Using array in bash (loop):

To loop thru array elements in for loop we use asterisk character * to get all elements:
for dir in ${dirs[*]}
echo "$dir"

Above example will display etc and www splitted by new lines.

Adding element to array


Creating empty array