Louis I. answered 05/27/19
Computer Science Instructor/Tutor: Real World and Academia Experienced
Absolutely, although in bash, you can do this with or without arrays ...
Let's use the command substitution output for all visible files in the current directory - "ls"
See below - without using arrays.
$ for file in $(ls)
> do
> echo $file
> ls -l $file
> done
1
-rw-rw-r--+ 1 lji None 0 May 25 09:10 1
2
-rw-rw-r--+ 1 lji None 0 May 25 09:10 2
3
-rw-rw-r--+ 1 lji None 0 May 25 09:10 3
collections.py
-rw-rw-r--+ 1 lji None 1210 May 25 11:26 collections.py
dict.py
-rw-rw-r--+ 1 lji None 625 May 25 11:34 dict.py
tester
-rw-rw-r--+ 1 lji None 66 May 27 12:12 tester
See below using array:
$ for file in ${fileList[@]}
do
echo $file
ls -l $file
done
1
-rw-rw-r--+ 1 lji None 0 May 25 09:10 1
2
-rw-rw-r--+ 1 lji None 0 May 25 09:10 2
3
-rw-rw-r--+ 1 lji None 0 May 25 09:10 3
collections.py
-rw-rw-r--+ 1 lji None 1210 May 25 11:26 collections.py
dict.py
-rw-rw-r--+ 1 lji None 625 May 25 11:34 dict.py
tester
-rw-rw-r--+ 1 lji None 66 May 27 12:12 tester
Of course, we can use the fileList array as a typical indexed array ... so if we wanted to display the 4th item in the array, could invoke the following:
$ echo ${fileList[3]} ## sub-3 references the 4th array element
collections.py