Free Hosting Online for WorkStations

< Previous | Contents | Next >

Adding Elements To The End Of An Array

Knowing the number of elements in an array is no help if we need to append values to the end of an array, since the values returned by the * and @ notations do not tell us the maxi- mum array index in use. Fortunately, the shell provides us with a solution. By using the

+= assignment operator, we can automatically append values to the end of an array. Here,


we assign three values to the array foo, and then append three more.


[me@linuxbox ~]$ foo=(a b c) [me@linuxbox ~]$ echo ${foo[@]} a b c

[me@linuxbox ~]$ foo+=(d e f) [me@linuxbox ~]$ echo ${foo[@]} a b c d e f

[me@linuxbox ~]$ foo=(a b c) [me@linuxbox ~]$ echo ${foo[@]} a b c

[me@linuxbox ~]$ foo+=(d e f) [me@linuxbox ~]$ echo ${foo[@]} a b c d e f


Top OS Cloud Computing at OnWorks: