In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers. For example, I can write a bash shell script that prints the integers between 1 and 10:
#!/bin/bash
for i in {1..10}
do
echo $i
done
Is it possible to instead iterate over a loop control variable that is a string, if I provide a list of strings? For example, suppose that I have a string fname that represents a file name. I want to call a set of commands for each file name. For example, I might want to print the contents of fname using a command like this:
#!/bin/bash
for fname in {"a.txt", "b.txt", "c.txt"}
do
echo $fname
done
In other words, on the first iteration, fname should have the value fname="a.txt", while on the second iteration, fname should have the value fname="b.txt", and so on. Unfortunately, it seems that the above syntax is not quite correct. I would like to obtain the output:
a.txt
b.txt
c.txt
but when I try the above code, I obtain this output:
{a.txt,
b.txt,
c.txt}
Can you please help me determine the correct syntax, so that I can iteratively change the value/contents of the variable fname? Thank you for your time.
{}, you don't need anything to loop over a (space-delimited) list{}and the,s. The alternative is to remove the spaces. So either"a.txt" "b.txt" "c.txt"or{"a.txt","b.txt","c.txt"}. But I prefer{a..c}.txtinstead.