OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Deleting An Array

To delete an array, use the unset command:


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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f

[me@linuxbox ~]$ unset foo

[me@linuxbox ~]$ echo ${foo[@]}


[me@linuxbox ~]$

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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f

[me@linuxbox ~]$ unset foo

[me@linuxbox ~]$ echo ${foo[@]}


[me@linuxbox ~]$


unset may also be used to delete single array elements:


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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f

[me@linuxbox ~]$ unset 'foo[2]' [me@linuxbox ~]$ echo ${foo[@]} a b d e f

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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f

[me@linuxbox ~]$ unset 'foo[2]' [me@linuxbox ~]$ echo ${foo[@]} a b d e f


In this example, we delete the third element of the array, subscript 2. Remember, arrays start with subscript zero, not one! Notice also that the array element must be quoted to prevent the shell from performing pathname expansion.

Interestingly, the assignment of an empty value to an array does not empty its contents:



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

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

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

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


Any reference to an array variable without a subscript refers to element zero of the array:



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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f [me@linuxbox ~]$ foo=A

[me@linuxbox ~]$ echo ${foo[@]}

A b c d e f

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

[me@linuxbox ~]$ echo ${foo[@]}

a b c d e f [me@linuxbox ~]$ foo=A

[me@linuxbox ~]$ echo ${foo[@]}

A b c d e f


Top OS Cloud Computing at OnWorks: