I’m always forgetting the syntax to make “for” loops in Bash. I guess it serves me right for using foreach most of my UNIX life instead. Anyhow, I know I will have to come back here to find it, so I thought I would write put up this quick example with the hope that it will be useful to others as well.
for i in $(seq 1 100); do echo -n "file${i} "; touch file${i} 2>&1; done
The the above for loop will create 100 files (called file1, file2, etc.).
People who looked at this item also looked at…
Related items
This entry was posted
on Monday, July 23rd, 2007 at 3:45 pm and is filed under Data and Technology.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Thank you! I just love it when people leave little blog entries like this one. You saved me 5 or 10 minutes looking up in the bash manual how to do this.
I agree with Todd!
Thank you very much!
Nice one!
Was useful to me, thanks!
Thanks very much! Never would have figured seq 1 10 needed to be $(seq 1 10)!!:)
Agreed, Thanks for posting.
Thanks for this, I couldn’t find anywhere how to generate n numbers, seq worked like a charm.
How about “touch file{1..100}” as an easier solution using bash.
Thank you very much for the code.
Nice and helpful..
Cheers!
Haha, took a look at a couple of website just before happening upon yours. There’s no way I would have been able to figure this one out so quickly (if at all) if it hadn’t have been for your great blurb on the subject here. Thanks.
What about:
for (( i=1;i&1; done
I guess for huge loops like 1000 or 100 000 this might work better.
Sorry my code was messed up in my previous comment. Hopefully it is OK this time:
for (( i=1;i<=100;i+=1 )) ; do echo -n "file${i} "; touch file${i} 2>&1; done
kobit why double parenthesis is needed?
eloy: you can found a answer here :
http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/dblparens.html
so accully you can use a C-style in double parenthesis
P.S.: Google is your friend
ei thanks! very useful!
How I can write loop without nubmers but with some strings like ‘a’, ‘b’, ‘c’?
Just came across this, but just for completeness:
Regarding using strings instead of numbers:
touch file{a..c}
or
for i in {a..c}; do something…; done
or
{’a',’b',’c',’etc’}
None of these work with solaris 5.8 bash…it doesnt have seq and i cant figure it out…VERY frustrating
This example parsing the output of seq to an array is considered very bad practice. seq is a non-standard unix/linux program and often isn’t included in many distros.
The proper technique is to use either for-in as an iterator, or to use c-style looping as was mentioned in a few comments.
Dan,
Not arguing your point, but the purpose of this was never to provide the world with a script that creates numbered files. The only real point was to help me remember how to do something for each line of output from some random program. What can I say, I have a bad memory.
Can you provide a script that would read a text file list of companies, and batch create individual text files for each company? TIA!
Never mind, I just converted my list to a comma-separated list and used…
touch {’comma’,’separated’,'list’}.txt
more of a lambda approach
seq 1 100|while read i; do echo -n “file${i} “; touch file${i} 2>&1; done
with letters you can
cat<<EOF|while read line; do echo -n “file$line “; touch file$line; done
first line
second line
another
another
EOF
or create a file delimited by new lines
cat thefile|while read line; do echo -n “file$line “; touch file$line; done
Hi,
When i executed ” for (( i=1;i&1; ”
I got an error like: ” ./forloop.ksh[3]: syntax error at line 3 : `((’ unexpected ”
Request you to please provide me a solution for FOR LOOP with range like in C.
Regards
Balaji K
Hi,
I got solution fro the above, i ran that in ksh, now i ran that under bash, now it is working fine.
For Loop in bash, pls browse:
http://tldp.org/LDP/abs/html/loops1.html
Regards
Balaji K
seq is very helpful. A related post
http://unstableme.blogspot.com/2008/12/ways-of-writing-bash-for-loop.html