• 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 have 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: