Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


for i in {1..10}
 echo $i

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:


for fname in {"a.txt", "b.txt", "c.txt"}
 echo $fname

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:




but when I try the above code, I obtain this output:




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.

share|improve this question
Remove the {}, you don't need anything to loop over a (space-delimited) list – Mat Sep 8 '12 at 15:54
@Mat means remove the {} 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}.txt instead. – manatwork Sep 8 '12 at 16:20
up vote 15 down vote accepted

The correct syntax is as follows:


for fname in a.txt b.txt c.txt
  echo $fname
share|improve this answer
Also, assuming an array of names fnames=( a.txt b.txt c.txt ) you can use the syntax for f in ${fnames[@]}; do echo $f; done. – user13742 Sep 8 '12 at 19:12
Is it true that for fname in a.txt b.txt c.txt and for fname in "a.txt" "b.txt" "c.txt" yield identical results? – Andrew Sep 8 '12 at 19:39
Andrew, yes it is true. They will yield identical results – Ali Gangji Sep 8 '12 at 20:29
Of course you should use for f in "${fnames[@]}"; do echo $f; done (with quotes around ${fnames[@]}) if the fnames values might contain whitespace.  And you should use "$f", especially if you're doing anything more sophisticated than echo (e.g., cat or cp). (And even if you're only doing echo, you should use printf instead.) – Scott Apr 23 at 21:02

Seems to me you should just do...

printf %s.txt\\n a b c
share|improve this answer
I like this even though the OP likely had something in mind to do other than echo before he was done. – Elder Geek Feb 4 at 18:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.