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

30 thoughts on “Bash For loop Example

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

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

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

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

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

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

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

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