0% found this document useful (0 votes)
38 views

Accessing Array Values

To access a value in an array, the array name and index are specified using ${array_name[index]}. Individual values can be accessed or all values using ${array_name[*]} or ${array_name[@]}. An example demonstrates defining an array with names and accessing the first and second indexes.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Accessing Array Values

To access a value in an array, the array name and index are specified using ${array_name[index]}. Individual values can be accessed or all values using ${array_name[*]} or ${array_name[@]}. An example demonstrates defining an array with names and accessing the first and second indexes.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Accessing Array Values

After you have set any array variable, you access it as follows −
${array_name[index]}
Here array_name is the name of the array, and index is the index of the value to be
accessed. Following is an example to understand the concept −
Live Demo
#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"

The above example will generate the following result −


$./test.sh
First Index: Zara
Second Index: Qadir
You can access all the items in an array in one of the following ways −
${array_name[*]}
${array_name[@]}
Here array_name is the name of the array you are interested in. Following example
will help you understand the concept −
Live Demo
#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"

The above example will generate the following result −


$./test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy

You might also like