• 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.).

    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.
  • 29 Comments

    Take a look at some of the responses we've had to this article.

    1. 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.

    2. Dec 2nd
      Reply

      I agree with Todd!

      Thank you very much!

    3. Chris Purcell
      Dec 20th
      Reply

      Nice one!

    4. Mar 20th
      Reply

      Was useful to me, thanks!

    5. Emily
      Mar 21st
      Reply

      Thanks very much! Never would have figured seq 1 10 needed to be $(seq 1 10)!!:)

    6. Nate
      Mar 24th
      Reply

      Agreed, Thanks for posting.

    7. Mark
      Mar 25th
      Reply

      Thanks for this, I couldn’t find anywhere how to generate n numbers, seq worked like a charm.

    8. Apr 9th
      Reply

      How about “touch file{1..100}” as an easier solution using bash.

    9. Apr 9th
      Reply

      Thank you very much for the code. :P
      Nice and helpful..

    10. Apr 17th
      Reply

      Cheers! :-)

    11. Apr 18th
      Reply

      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.

    12. kobit
      May 7th
      Reply

      What about:

      for (( i=1;i&1; done

      I guess for huge loops like 1000 or 100 000 this might work better.

    13. kobit
      May 7th
      Reply

      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

    14. eloy
      May 22nd
      Reply

      kobit why double parenthesis is needed?

    15. Duncan
      Jun 10th
      Reply

      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 :)

    16. jica
      Jul 30th
      Reply

      ei thanks! very useful! :D

    17. Aug 21st
      Reply

      How I can write loop without nubmers but with some strings like ‘a’, ‘b’, ‘c’?

    18. Chris
      Jan 17th
      Reply

      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’}

    19. David
      Feb 2nd
      Reply

      None of these work with solaris 5.8 bash…it doesnt have seq and i cant figure it out…VERY frustrating

    20. Dan
      Mar 12th
      Reply

      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.

      • Mar 22nd
        Reply

        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.

    21. Scott Gardner
      Apr 4th
      Reply

      Can you provide a script that would read a text file list of companies, and batch create individual text files for each company? TIA!

      • Scott Gardner
        Apr 4th
        Reply

        Never mind, I just converted my list to a comma-separated list and used…

        touch {‘comma’,’separated’,'list’}.txt

    22. Jeff Price
      Apr 13th
      Reply

      more of a lambda approach

      seq 1 100|while read i; do echo -n “file${i} “; touch file${i} 2>&1; done

    23. Jeff Price
      Apr 13th
      Reply

      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

    24. Balaji
      Apr 15th
      Reply

      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

    25. Balaji
      Apr 15th
      Reply

      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

    26. omi
      Nov 2nd
      Reply

      thanks a lot !!!!

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: bash for (15324) - bash for loop (7911) - t (7866) - for bash (7682) - bash for in (3168) - bash foreach (2760) - for loop bash (2257) - bash script for loop (1735) - bash loop (1550) - bash for example (1388) - for in bash (958) - foreach bash (893) - unix for loop (888) - bash for each (870) - bash script loop (837) - bash for loops (770) - bash foreach loop (767) - for loop in bash (628) - bash for i in (425) - bash example (423) - unix script for loop (409) - bash loops (373) - bash for loop example (359) - bash loops (351) - for loop unix (336) - unix bash for loop (332) - unix for loop syntax (315) - bash for in loop (312) - bash script foreach (306) - f (279) - bash for each loop (278) - bash foreach example (269) - for loop syntax in unix (267) - loop bash (245) - for each bash (236) - for i in bash (228) - for bash example (225) - unix for loop example (221) - for loops bash (200) - bash for examples (195) - bash examples (193) - bash for i (189) - for loop in unix script (187) - bash list (186) - bash for loop syntax (183) - seq solaris (169) - bash shell foreach (162) - bash script for (151) - bash seq (149) - for loops in bash (147) - bash script for each (145) - foreach loop bash (142) - bash scripting foreach (137) - for loop unix script (137) - bash for loop list (134) - bash for loop seq (132) - unix bash for (129) - script bash for (127) - bash for loop examples (120) - unix for loop examples (120) - bash looping (120) - for loop example (119) - bash for in list (117) - bash scripting FOR loop (116) - bash for 1 to 10 (116) - bash script example (114) - for loop + unix (113) - bash script seq (106) - bash loop for (105) - bash script for loop example (105) - solaris seq (105) - unix script loop (104) - unix loop (103) - unix for loops (102) - bash for seq (101) - bash script list (101) - bash for syntax (101) - foreach bash script (100) - bash foreach syntax (99) - foreach bash example (98) - loop in bash (94) - foreach in bash (92) - bash example for (90) - for loop bash script (87) - for each loop bash (85) - for loop examples in unix (83) - bash for loop in (81) - unix bash loop (80) - for loop examples (79) - bash script loops (79) - for loop syntax unix (79) - for bash loop (77) - shell for loop example (77) - bash for loop files (77) - solaris bash for loop (76) - for each in bash (74) - for loop in unix (74) - for loop seq (73) - bash for i in list (68) - bash loop example (67) - bash loop script (66) - bash loop list (66) - unix bash foreach (66) - bash for each in (65) - bash for each file (63) - bash scripts for loop (62) - loop for bash (61) - for loop unix bash (60) - for bash script (60) - for i bash (59) - bash loop seq (59) - unix loops (59) - for i in 1 to 10 bash (59) - loop bash script (57) - for (( loop in unix (56) - bash loop files (56) - bash script "for each" (56) - for loop in bash script (55) - unix for loop bash (54) - bash for list (54) - unix script for loop example (54) - syntax of for loop in unix (53) - loops bash (51) - for loop in solaris (51) - for bash seq (51) - for loop example in UNIX (51) - loops in bash (49) - bash for 1 to 100 (49) - bash script for loops (49) - foreach loop in bash (49) - bash scripts (48) - unix for in loop (48) - bash script loop files (47) - bash script examples (47) - mss (46) - bash shell for loop (45) - "For Loop" Bash Script (44) - for loop in bash shell (44) - bash for (( (44) - bash for loop script (43) - bash for each example (43) - bash for loop file (43) - foreach BASH shell (43) - syntax for for loop in unix (43) - bash for loop 1 to 10 (42) - bash for in in (42) - example of for loop in unix (41) - solaris for loop (41) - For loop examples unix (40) - for loop unix examples (40) -