I am trying to loop through filenames within a directory so I can perform an operation on each.
I am able to loop through just 10 filenames if I use:
var=($(ls directory))
for k in {1..10}
do
echo ${var[$k]}
done
Which gives me the first 10 filenames in my directory. However, I want to loop through them all.
If I do:
length=($(ls directory -1 | wc -l))
I get the length (for this example, it is 62)
So when I combine everything together:
var=($(ls directory))
length=($(ls directory -1 | wc -l))
for k in {1..$length}
do
echo ${var[$k]}
done
I get the error 'syntax error: operand expected (error token is "{1..62}")
Any help to fix this, or a better solution would be greatly appreciated!
{1..$length}is doomed to failure.