O'Reilly logo

The Linux Command Line by William E. Shotts Jr.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Array Operations

There are many common array operations. Such things as deleting arrays, determining their size, sorting, and so on have many applications in scripting.

Outputting the Entire Contents of an Array

The subscripts * and @ can be used to access every element in an array. As with positional parameters, the @ notation is the more useful of the two. Here is a demonstration:

[me@linuxbox ˜]$ animals=("a dog" "a cat" "a fish")
[me@linuxbox ˜]$  for i in ${animals[*]}; do echo $i; done
a
dog
a
cat
a
fish
[me@linuxbox ˜]$  for i in ${animals[@]}; do echo $i; done
a
dog
a
cat
a
fish
[me@linuxbox ˜]$  for i in "${animals[*]}"; do echo $i; done
a dog a cat a fish
[me@linuxbox ˜]$  for i in "${animals[@]}"; do echo $i; done
a dog
a cat
a fish

We create ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required