Different way to initialize array in bash shell.

p13.sh

#!/bin/bash

array1[0]=one
array1[1]=1
echo ${array1[0]}                         //print one                          
echo ${array1[1]}                          //print 1

array2=( one two three )
echo ${array2[0]}                            //one
echo ${array2[2]}                           //three

array3=( [9]=nine [11]=11 )
echo ${array3[9]}                           //nine
echo ${array3[11]}                         //11

read -a array4
for i in "${array4[@]}"                   //wait for user to give input and stores in array4[]
do
        echo $i                                     //prints user given input
done

exit 0


output:
./p13.sh
one
1
one
three
nine
11
u                                                   //user input
u


Post a Comment

Previous Post Next Post