how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array.

plz help me how to accomplish this.
Thanks in advance.

#! /bin/bash
i=0
for x in /home/sam/*.dat
do
    arr[$i]=$x
    i=$(($i+1))
done
echo ${arr[@]}
1 Like

Hello Bala ,

In addition to your solution , i would like to add the increment part of index , can also be done below .

((i+=1))
1 Like

Or just ((i++))

1 Like

Or just use:

FILES=( /home/sam/*.dat )
echo "${FILES[@]}"