Bash For loop Example

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

Tagged , , , , , ,

30 thoughts on “Bash For loop Example

  1. Todd Bradley says:

    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. Henrik says:

    I agree with Todd!

    Thank you very much!

  3. Chris Purcell says:

    Nice one!

  4. JonnyRo says:

    Was useful to me, thanks!

  5. Emily says:

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

  6. Nate says:

    Agreed, Thanks for posting.

  7. Mark says:

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

  8. BKC says:

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

  9. 4j4x says:

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

  10. bre says:

    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.

  11. kobit says:

    What about:

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

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

  12. kobit says:

    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

  13. eloy says:

    kobit why double parenthesis is needed?

  14. Duncan says:

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

  15. jica says:

    ei thanks! very useful! :D

  16. Nick says:

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

  17. Chris says:

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

  18. David says:

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

  19. Dan says:

    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.

    • cliff says:

      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.

  20. Scott Gardner says:

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

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

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

  21. Jeff Price says:

    more of a lambda approach

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

  22. Jeff Price says:

    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

  23. Balaji says:

    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

  24. Balaji says:

    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

  25. omi says:

    thanks a lot !!!!

  26. Fred says:

    II was able to get this with the help of a friend it works &hope it works for you as well
    # script that will open a table file into a text file

    CMD=”select * from_product table ”

    echo $CMD

    mysql -database username -pNnwxx9Kj -database name -e “${CMD}” > outsql ;

    if [ $? -eq 0 ]
    then
    echo success
    else
    echo failure
    fi

    cat $outsql

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>