How to append values to the list in shell script..?

#!/bin/bash cd /pg file=`ls -l |awk '{print $9}'` list=() for i in $a do echo $i a=`cat /pg/$i | head -n 1 |awk '{print $8}' ` #output: first interation 15 & 2nd values 60 list+=($a) #a1=`cat /pg/$i | head -n 2 |awk '{print $8}'` done echo $list 

through this a=`ls -l |awk '{print $9}'` I'm getting two file and iterating through for loop and appending values to list() the list should contains "15","16" but list contains only one value is "15" Please help me to fix the same.

1

2 Answers

To access the whole array, use

echo "${list[@]}" 

Using just $list is equivalent to ${list[0]}, i.e. it only shows the first element.

# create list hosts1=() hosts2=() # add hosts hosts1+=( host1 ) hosts1+=( host2 ) hosts2+=( host3 ) hosts2+=( host4 ) # combine 2 lists hosts1+=( ${hosts2[@]} ) # add some more echo ${hosts1[@]} hosts1+=( host5 ) hosts1+=( host6 ) echo ${hosts1[@]} 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like