Bash For Loop Examples

last updated in Categories , , , , , , , ,

How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.
Bash for loop examples for Macos/Linux and Unix

for loop syntax

Numeric ranges for syntax is as follows:

for VARIABLE in 1 2 3 4 5 .. N


for VARIABLE in file1 file2 file3
	command1 on $VARIABLE


for OUTPUT in $(Linux-Or-Unix-Command-Here)
	command1 on $OUTPUT
	command2 on $OUTPUT


This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

for i in 1 2 3 4 5
   echo "Welcome $i times"

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:

for i in {1..5}
   echo "Welcome $i times"

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
     echo "Welcome $i times"

Sample outputs:

Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

The seq command (outdated)

WARNING! The seq command print a sequence of numbers and it is here due to historical reasons. The following examples is only recommend for older bash version. All users (bash v3.x+) are recommended to use the above syntax.

The seq command can be used as follows. A representative example in seq is as follows:

for i in $(seq 1 2 20)
   echo "Welcome $i times"

There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq. The builtin command are fast.

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).

for (( EXP1; EXP2; EXP3 ))

A representative three-expression example in bash as follows:

for (( c=1; c<=5; c++ ))
   echo "Welcome $c times"

Sample output:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:

for (( ; ; ))
   echo "infinite loops [ hit CTRL+C to stop]"

Conditional exit with break

You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:

for I in 1 2 3 4 5
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  if (disaster-condition)
	break       	   #Abandon the loop.
  statements3          #While good and, no disaster-condition.

Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.

for file in /etc/*
	if [ "${file}" == "/etc/resolv.conf" ]
		countNameservers=$(grep -c nameserver /etc/resolv.conf)
		echo "Total  ${countNameservers} nameservers defined in ${file}"

Early continuation with continue statement

To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.

for I in 1 2 3 4 5
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  if (condition)
	continue   #Go to next iteration of I in the loop and skip statements3

This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.

for f in $FILES
        # if .bak backup file exists, read next file
	if [ -f ${f}.bak ]
		echo "Skiping $f file..."
		continue  # read next file and skip the cp command
        # we are here means no backup file exists, just use cp command to copy file
	/bin/cp $f $f.bak

Check out related media

This tutorial is also available in a quick video format. The video shows some additional and practical examples such as converting all flac music files to mp3 format, all avi files to mp4 video format, unzipping multiple zip files or tar balls, gathering uptime information from multiple Linux/Unix servers, detecting remote web-server using domain names and much more.

Video 01: 15 Bash For Loop Examples for Linux / Unix / OS X Shell Scripting


You learned how to use the bash for loop with various example. See the following resources for more info.

  • See all sample for loop shell script in our bash shell directory
  • Bash for loop syntax and usage page from the Linux shell scripting wiki
  • man bash
  • help for
  • help {
  • help break
  • help continue

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at

Historical Comment Archive

254 comment

  1. Nice one. All the examples are explained well, thanks Vivek.

    seq 1 2 20
    output can also be produced using jot

    jot – 1 20 2

    The infinite loops as everyone knows have the following alternatives.

    while :


  2. The last example can also be produced without the ” in $FILES”:


    for f

    # For-Loop body


    If the ” in …” is excluded, the loop will run as if “in $@” was given.

  3. hey vivek i tried the following syntax for for loop suggested by u but both dint work…
    for (( c=1; c<=5; c++ ))
    echo “Welcome $c times…”

    for i in {1..5}
    echo “Welcome $i times”

    got error for both the syntax
    1. unexpected ‘(‘
    2. it printed welcome {1..5} times instead repeating it…


    1. hi manish your both coding are correct… before execute you must give the execution permission for that file… so you try following steps…
      1.goto terminal
      2. vim simple
      3.then write the following code..

      for (( c=1; c<=5; c++ ))
      echo “Welcome $c times…”

      4.then save and quit
      5.chmod 744 simple
      6. ./simple

      i hope surely it will help you…

    2. I can help you on 2. — You were not using the Bash 3.0 or higher. Upgrade your bash and it will work.

    3. Hi All,

      I have some ‘.gif’ and ‘.jpg’ files in a directory named Pictures in my home directory. I need to write bash script that would create 2 separate html files such as page1.html and page2.html one for gif files and the other for jpg files. And when i execute the script i need to have the html files in the Pictures directory and should have the contents as follows:



      Please help me out. Thanks in advance

      1. your problem is very easy to solve using the examples on the page.

        in addition to the for loops, you will need to use the echo command, the redirection operator >> and a basic knowledge of html.

        your script should do:

        1. create a html file with the header, opening body tags etc.
        2. have a loop for all jpg files
        2.1 inside the loop, print one line with the html code for an image, using the image’s filename
        3. close the loop, add closing html tags
        4-6. same as 1-3, but for .gif instead of .jpg

  4. i tried the last example but i seen dint work

    set -x
    CP=$(which cp)
    for f in $FILES
            if [ -f ${f}.bak ]
                    echo "skiping $f file"
                    continue # read netxt file and skip cp command
            $CP $f $f.bak

    i would like know where is the error

    1. Maybe correcting your misspelled variable ”FILLES” would be a step forward?

  5. hi guys . can any one help me . i need a script to check the file /var/log/messages every 10 minutes .and if its has the following log :
    ext3_orphan_cleanup: deleting unreferenced

    to apply the following command
    sendsms to wut ever .

    thnx alot

  6. i would like to breakk a csv file depending upon two criteria.

    1. SIngle file should not be more than 100 lines
    2. The third column if has same value on the 100th line as that of the 101th line, the complete line should be included in the 2nd file.
    so., now, 1st file will have 99 lines and 2nd file will have 100 lines, ifthe above 2nd condition does not repeats.,

  7. for file in $(ls 0902*0010202.TLG); do
    day=$(echo $file | cut -c 1-6)
    grep ^203 $file | cut -d, -f3 | sort | uniq -c | while read line; do
    cnt=$(echo $line | cut -d” ” -f1)
    acct=$(echo $line | cut -d” ” -f2)
    echo “Date 20${day} Account ${acct} had ${cnt} 203’s” >> Feb_report.txt

    when i run it it gives me a syntax error
    ins@ARTMGA01> ./
    ./ syntax error at line 4: `$’ unexpected

    could you help

  8. Hi

    How do I read line by line in a file, and use these in a loop? I have a file I read in (cmd max_cpu):
    firefox 15
    conky 1
    cmds=$(cat file)
    But $cmds now consist of n items, all being “equal” – it does not split on each line to a new array. I expected that by looping over $cmds, I’d get a 2D array…. I did not.

    Otherwise, excellent tutorial!

    1. Try:

      while read line
      	# store field 1
      	F1=$(echo $line|cut -d$FS -f1)
      	# store field 2
      	F2=$(echo $line|cut -d$FS -f6)
      	# store field
      	F3=$(echo $line|cut -d$FS -f7)
      	echo "User \"$F1\" home directory is $F2 and login shell is $F3"
      done < $FILE
      1. I know this is an ancient thread, but thought this trick might be helpful to someone:

        For the above example with all the cuts, simply do

        set `echo $line`

        This will split line into positional parameters and you can after the set simply say

        F1=$1; F2=$2; F3=$3

        I used this a lot many years ago on solaris with “set `date`”, it neatly splits the whole date string into variables and saves lots of messy cutting :-)

        … no, you can’t change the FS, if it’s not space, you can’t use this method

    2. Hi Vivek,
      Please help I was trying to use your code in a script similar issue trying to use a csv file with three columns (loginname,surname,firname) as input for a file that will be executed

      #file to be changed
      echo “your login name is $loginn, your surname is $ssn and your firname $ffn”


      while read LINE;
      LINNUM=`expr $LINENUM + 1`
      done < smallops.csv

      while read LINE;
      #store field 1
      F1=$(echo $line|cut -d$FS -f1)
      #store field 2
      F2=$(echo $line|cut -d$FS -f6)
      #store field 3
      F3=$(echo $line|cut -d$FS -f7)
      sed '{$LINNUM s/lgn/$F1/g; $LINNUM s/ssn/$F2/g; $LINNUM s/ffn/$F3/g; }' -i smallops.csv
      done < g.csv


      1. @Alvin

        First, this code won’t work at least because it does not input fileA.csv as intended.

        Second, to debug, try to break down this program, piece by piece.

        For instance, it could read input more easily from fileA.csv this way:
        (I have not included your [sed] instruction yet. Go step by step)

        # backup current IFS (Internal File Separator)
        # change IFS to directly read input file into 3 variables a,b,c
        while read a b c
        echo "LOGIN:$a LASTNAME:$b FIRSTNAME:$c"
        done < fileA.csv
        # restore IFS

        Third, try to explain what you would like to do with you [sed] instruction.

        Why do you start your LINENUM at 4?

        What is the content of your smallops.csv? What is it for?

        By the way, your [sed] instruction seems to contain a mispelled search pattern, for first field $F1, should’nt it be [loginn] instead of [lgn] ?

        You mention only ONE csv file, but your code contains: fileA.csv, smallops.csv, and g.csv ? What are they? Typos ? Errors in your code?

        1. Sorry, (I forgot a HTML code TAG) Complete code is:

          # backup current IFS (Internal File Separator)
          # change IFS to directly read input file into 3 variables a,b,c
          while read a b c
          echo "LOGIN:$a LASTNAME:$b FIRSTNAME:$c"
          done < fileA.csv
          # restore IFS
  9. Hi Vivek,
    Thanks for this a useful topic.

    IMNSHO, there may be something to modify here
    Latest bash version 3.0+ has inbuilt support for setting up a step value:

    for i in {1..5}
    1) The increment feature seems to belong to the version 4 of bash.
    Accordingly, my bash v3.2 does not include this feature.

    BTW, where did you read that it was 3.0+ ?
    (I ask because you may know some good website of interest on the subject).

    2) The syntax is {} where from, to, step are 3 integers.
    You code is missing the increment.

    Note that GNU Bash documentation may be bugged at this time,
    because on GNU Bash manual, you will find the syntax {x..y[incr]}
    which may be a typo. (missing the second “..” between y and increment).


    The Bash Hackers page
    again, see
    seeems to be more accurate,
    but who knows ? Anyway, at least one of them may be right… ;-)

    Keep on the good work of your own,
    Thanks a million.

    — Peko

  10. Yes.

    But you mispelled the syntax with an extra dot “.” after “START’

    — Peko

  11. Hello,

    is there a simple way to control the number formatting? I use several computers, some of which have non-US settings with comma as a decimal point. This means that
    for x in $(seq 0 0.1 1) gives 0 0.1 0.2 … 1 one some machines and 0 0,1 0,2 … 1 on other.
    Is there a way to force the first variant, regardless of the language settings? Can I, for example, set the keyboard to US inside the script? Or perhaps some alternative to $x that would convert commas to points?
    (I am sending these as parameters to another code and it won’t accept numbers with commas…)

    The best thing I could think of is adding x=`echo $x | sed s/,/./` as a first line inside the loop, but there should be a better solution? (Interestingly, the sed command does not seem to be upset by me rewriting its variable.)


  12. To Michal Kaut:

    Hi Michal,

    Such output format is configured through LOCALE settings.

    I tried :

    export LC_CTYPE=”en_EN.UTF-8″; seq 0 0.1 1

    and it works as desired.

    You just have to find the exact value for LC_CTYPE that fits to your systems and your needs.


  13. Comment 12 was really helpful. I was trying to split up a log file by date, such as
    logfile.20091026 , without having to use grep a million times. I’m kind of disappointed I couldn’t find a one-liner to do so, but I will take what I can get :).

  14. @Brad,

    Try this without grep or cut using bash parameter expansion :

    echo $log
    echo $date


  15. I think you misunderstood. I’m going line by line, and converting the dates at the beginning of the line, such as “Sep 12”, and copying that line from logfile to logfile.20090912.

    My script is really slow though, with the conversion of the month name to a number. I’ve tried using the date command, and my own function, and both take 7 seconds to process 10,000 lines. It doesn’t seem like a long time, but I’ve got a lot of log files to process on multiple machines.

    I don’t guess you’d know a faster trick, would you?

  16. @Brad, yes, I did misunderstood your post. If I were you I will try out awk.

    Thanks for the heads up. The faq has been updated.

  17. @Peko:

    (I’m the operator of, that’s why I found this page):

    Regarding Bash documentation for brace expansion (increment syntax), actually I’m right and the documentation is wrong (a rare situation!). I reported it to the list.

  18. To Vivek:
    Regarding your last example, that is : running a loop through arguments given to the script on the command line, there is a simplier way of doing this:
    # instead of:
    # FILES=”$@”
    # for f in $FILES

    # use the following syntax
    for arg
    # whatever you need here – try : echo “$arg”

    Of course, you can use any variable name, not only “arg”.

    To TheBonsai: Welcome Buddy!
    Fine! I am happy to see 2 great FOSS web sites now related !

  19. Command line while loop.. Very handy..

    Say you wanted to rename all the files in a specific dir..
    Create a file with the contents you want to rename
    (ls -l | awk ‘{print $9}’ > asdf or something)

    Contents of asdf:

    cat asdf | while read a ; do mv $a $ ; done

    ls -l

    I have used this while command for many things from simply renaming files to formatting and labling new SAN luns..

    1. There are much easier ways to do this – also it works only for extensions. How do you change the middle of the file name or a few characters on the left?

      Here is the regular way of what you just did:

      for i in *; do mv $i $; done

  20. There are 2 problems and one optical flaw with your code:

    (1) You should use read -r without any variable name given, to use the default $REPLY (due to a specific behaviour of read, see manpage)
    (2) You should quote $a
    (3) Useless use af cat :)

  21. To tdurden:

    Why would’nt you use

    1) either a [for] loop
    for old in * ; do mv ${old} ${old}.new; done

    2) Either the [rename] command ?
    excerpt form “man rename” :

    RENAME(1) Perl Programmers Reference Guide RENAME(1)

    rename – renames multiple files

    rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

    “rename” renames the filenames supplied according to the rule specified
    as the first argument. The perlexpr argument is a Perl expression
    which is expected to modify the $_ string in Perl for at least some of
    the filenames specified. If a given filename is not modified by the
    expression, it will not be renamed. If no filenames are given on the
    command line, filenames will be read via standard input.

    For example, to rename all files matching “*.bak” to strip the
    extension, you might say

    rename ‘s/\.bak$//’ *.bak

    To translate uppercase names to lower, you’d use

    rename ‘y/A-Z/a-z/’ *

    — Philippe

  22. Note for rename(1): There exist two major variants on Linux system. One non-Perl originating in the RedHat area, and one Per, originating in the Debian area.

  23. To tdurden:

    I would also replace “ls -l | awk ‘{print $9}'” with just “ls”. Otherwise you’ll run into issues with files that have spaces in it. As far as using:

    for i in *;
    for i in $(ls);

    I personally prefer “$(ls)” or “$(find . )”. This provides more control over what files I’m going to be looping through. For instance:

    $(ls -F | grep -v “\/$”)
    $(ls -A)

    1. #!/bin/bash
      echo “Bash version ${BASH_VERSION}…”
      for i in {0..10..2}
      echo “Welcome to my new script $i times”

      help ……………………….. :)

  24. To Sean:
    CMIIAW :

    try the following commands:
    # touch “file with spaces in name”
    # for f in *; do echo “”;done

    which shows that there is no need to use [for f in $(ls)] instead of [ for f in *]
    Doesn’t it ?

    — Philippe

  25. Sorry Sean, my last post was truncated,
    due to limitations of this form used to post comments. (impossible to use Greater_than and Less_than caracters)

    I meant, use the following:
    # touch “file with spaces in name”
    # for f in *; do echo “:${f}:”;done

    :file with spaces in name:

  26. Sorry for the confusion, I understand that “for i in *;” will not have any issues with spaces. I was referring to the ls -l | awk ‘{print $9}’ having issues with spaces. The reason I choose to use $(ls) instead of * is for filtering out unwanted files e.g. $(ls -F | grep -v “\/$”)

  27. To Sean:

    But then, that’s wrong.
    [ for f in $(ls -F|grep -v “V$”) ]
    won’t process appropriately spaces in filename.
    Check :
    # touch “file with spaces in name”
    # for f in $(ls -F|grep -v “V$”); do echo “:${f}:”;done

  28. The best tool to filter files and process them
    is [find] piped to [xargs] (with zero-ended filenames)

  29. To sean:
    But if you want to exclude files from globbing,
    [bash] has the [extglob] option.

    Let’s say you want to process every file except files ending by a “V”, just type

    # for f in !(*V); do echo “:${f}:”;done

  30. If you set the shell option extglob, Bash understands some more powerful patterns. Here, a is one or more pattern, separated by the pipe-symbol (|).

    ?() Matches zero or one occurrence of the given patterns
    *() Matches zero or more occurrences of the given patterns
    +() Matches one or more occurrences of the given patterns
    @() Matches one of the given patterns
    !() Matches anything except one of the given patterns

  31. I have two files here X.a and y.a Now what i need is i need to substitute CvfdsDisk_sdb/c/d/e in lines of Node CvfsDisk_XXX in the order CvfsDisk_sdb/c/f/g first word of each line of x.a exists. how can i do in shell scripting i can get the first word of each line of X.a using awk /cut but to replace these in y.a i am not getting it … any help here ?
    [Raj]$ cat x.a
    CvfsDisk_sdb /dev/sdb # host 0 lun 1 sectors 4840746976 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJBSXJFF92A9D003C6A
    CvfsDisk_sdc /dev/sdc # host 0 lun 0 sectors 3906148319 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJ8MJKFF92A9D001FEC
    CvfsDisk_sdf /dev/sdf # host 0 lun 1 sectors 4840746976 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJBSXJFF92A9D003C6A
    CvfsDisk_sdg /dev/sdg # host 0 lun 0 sectors 3906148319 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJ8MJKFF92A9D001FEC
    [naren@Beas dxall]$ cat y.a

    [StripeGroup Metafiles]
    Metadata Yes
    Status UP
    Read Enabled
    Write Enabled
    Journal Yes
    StripeBreadth 1280K
    Node CvfsDisk_sdb 0

    [StripeGroup datafiles1]
    Metadata Yes
    Status UP
    Read Enabled
    Write Enabled
    StripeBreadth 1024K
    Node CvfsDisk_sdc 0

    [StripeGroup datafiles2]
    Metadata Yes
    Status UP
    Read Enabled
    Write Enabled
    StripeBreadth 1280K
    Node CvfsDisk_sdd 0

    [StripeGroup datafiles3]
    Metadata Yes
    Status UP
    Read Enabled
    Write Enabled
    StripeBreadth 1024K
    Node CvfsDisk_sde 0

  32. To Philippe:

    You are right,

    # for i in $(ls)

    will break up files with spaces if IFS isn’t set to just the newline character. I don’t believe this is consistent across distributions. So the for loop should have

    # export IFS=$’\n’

    before it. I also use find in for loops when I want to look through the directory contents, but this isn’t always desired. Thanks for the info about extglob, I haven’t done much with extended globbing in bash.

  33. To Sean:
    Right, the more sharp a knife is, the easier it can cut your fingers…

    I mean: There are side-effects to the use of file globbing (like in [ for f in * ] ) , when the globbing expression matches nothing: the globbing expression is not susbtitued.

    Then you might want to consider using [ nullglob ] shell extension,
    to prevent this.

    Devil hides in detail ;-)

  34. Response to the tip number 12
    At thos script, It’s missing the followng line
    in the variables declaration
    (you forgit the delimiter field, for the cut command)

  35. #! /usr/bin/ksh
    for i in `cat /input`
    bdf | grep file_system | grep -vE ‘^A|B|C’ | awk ‘{ print $4}’ | while read output;
    file_system=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
    partition=$(echo $output | awk ‘{ print $2 }’ )
    if [ $file_system -ge 60 ]; then
    echo “don’t run the sync $partition ($file_system%) ”
    rsync $i
    The problem with the logic I’m having is I do not want the script to exit(as it does now) the loop once the file_system area reaches 60%. I want
    it to continue to retest bdf and continue the loop once disk usage drops below 60%.

    Any Ideas?

  36. To Rilif:

    1) I assume you use [ bdf ] on UNIX system – because Linux equivalent is [ df ] – and I cannot be of help because I cannot test your script on my Linux boxes.

    2) This seems to be a specific programming debugging problem and out of this topic scope – There may be a better place to post that kind of topic – A programmer forum for instance.

    Best regards.

  37. do any one know how to write this script in steps?
    as so /// ./ test 10 /// The first argument [1] will ex. to create a multiple users, groups, cn, dn, etc for ldap in one or two scripts but from command line. you would just enter file then the number of atrributes to build.
    this is a headache for me since i’m new at this.

  38. To Dee:

    0) The first part of your first sentence is incomprehensible – this may be because text that you entered is altered, it may contain HTML-like syntax that is interpreted by this comment form. (By the way, Vivek Gite would be welcomed to tell us how to prevent this. TIA :-) )

    1) LDAP syntax is off-topic.

    2) You’ll find appropriate resources in LDAP forums/sites – just find out.

    3) We may be in position to help you to build a [for] loop, assuming you do your part of the job by providing the basic LDPA instructions to create user, for instance. Try to create at least a LDAP
    object by yourself on the command-line, then provide us the code, and as much as possible further explanation please.

  39. Thanks Vivek – But I am afraid I do not get it right – what does “pre” mean ? (I understand you wrote the “less than” tag, and “greater than” tag – but why “pre” ?

    And are you sure these are the only ones two use ?

  40. @Philippe,

    All allowed html tags are displayed below the form itself. It is wordpress that converts those symbol and syntax is

    echo "Hello world!"


  41. first file make:

    echo "dn: $2 $3,o=dmacc,dc=bad1dee,dc=com" > $1
    echo "changetype: add" >> $1
    echo "objectclass: top" >> $1
    echo "objectclass: person" >> $1
    echo "objectclass: organizationalPerson" >> $1
    echo "objectclass: inetOrgPerson" >> $1
    echo "cn: $2 $3" >> $1
    echo "givenName: $2" >> $1
    echo "sn: $3" >> $1
    echo "uid: ${2:0:1}$3" >> $1
    echo "mail: $4" >> $1
    u1=`cat $1 | tr -d '\n'`
    #u2=`echo $u1 | tr \| '\n'`
    u2=`echo $u1 | sed s/\|/\\n/`
    echo "DBG"
    echo -e $u1
    echo "DBG"
    echo -e $u1 | ( IFS=, ; while read fname lname mail;
    echo -e "Creating $fname $lname\n\n"

    ./ ./user.ldif $fname $lname $mail

    cat ./user.ldif
    echo -e "\n\n"
    done )

    ./ mass_user.txt
    This ex: will pull from a list but same out come i do not know how to write another script to pull the attributes i need from the command line like ./test 100 and that command will pull only a 100 users id’s from idif.txt out of 1000 generated.

    This next samples of code will file in the attributes for you.
    first file make:

    echo "dn: $2 $3,o=dmacc,dc=bad1dee,dc=com" > $1
    echo "changetype: add" >> $1
    echo "objectclass: top" >> $1
    echo "objectclass: person" >> $1
    echo "objectclass: organizationalPerson" >> $1
    echo "objectclass: inetOrgPerson" >> $1
    echo "cn: $2 $3" >> $1
    echo "givenName: $2" >> $1
    echo "sn: $3" >> $1
    echo "uid: ${2:0:1}$3" >> $1
    echo "mail: $4" >> $1


    u1=`cat $1 | tr -d '\n'`
    #u2=`echo $u1 | tr \| '\n'`
    u2=`echo $u1 | sed s/\|/\\n/`
    echo "DBG"
    echo -e $u1
    echo "DBG"
    echo -e $u1 | ( IFS=, ; while read fname lname mail;
    echo -e "Creating $fname $lname\n\n"
    ./ ./user.ldif $fname $lname $mail
    cat ./user.ldif
    echo -e "\n\n"
    done )


    ./ adduserfile1.txt

    This is what i’m working on now ? i still do not know how to tie in C++ or bash script this code to work with command line, so i can control the out come of created users.

    Set objRootDSE = GetObject("LDAP://rootDSE")
    Set objContainer = GetObject("LDAP://cn=Users," & _
    For i = 1 To 1000
        Set objLeaf = objContainer.Create("User", "cn=UserNo" & i)
        objLeaf.Put "sAMAccountName", "UserNo" & i
    WScript.Echo "1000 Users created."
    example: [root]#./test 10
    usid:UserNo1 gid:manager cn:Bob dn:bHarison

    This script makes a 1000 users. However i can not control the out come. For example: from the command line I would like it to stop at 100 users by typing in ./test 100. Using agrv [1]. so when I type a number after the file name it will create a list and print that record to the screen. I do not know bash that well as C++ and it is not helping because the char.. are diff…

    Edited by admin. Reason: This is not a forum, this is a blog. If you need further help please try our forum

  42. To Dee:
    1) Man, with a 3-users-sample instead of hundreds, we would have figured out, don’t you think so?

    2) Well that’s a start. May be Vivek would like to wipe this post out, and create a new topic: “Of mice, LDAP and loops” ;-) ???

  43. There is an interesting difference between the exit value for two different for looping structures (hope this comes out right):
    for (( c=1; c<=2; c++ )) do echo -n "inside (( )) loop c is $c, "; done; echo "done (( )) loop c is $c"
    for c in {1..2}; do echo -n "inside { } loop c is $c, "; done; echo "done { } loop c is $c"

    You see that the first structure does a final increment of c, the second does not. The first is more useful IMO because if you have a conditional break in the for loop, then you can subsequently test the value of $c to see if the for loop was broken or not; with the second structure you can’t know whether the loop was broken on the last iteration or continued to completion.

  44. sorry, my previous post would have been clearer if I had shown the output of my code snippet, which is:
    inside (( )) loop c is 1, inside (( )) loop c is 2, done (( )) loop c is 3
    inside { } loop c is 1, inside { } loop c is 2, done { } loop c is 2

  45. Dmitry, please give a little more detail about what you are trying to achieve. You can see from my examples above that there is no problem to put a simple loop on one line. Basically you use semicolons (;) instead of line breaks.

  46. Dominic,
    thaks a lot for your quick answer. You have answered on my question but I’m still having problems. Please take a look at this.
    dmitry@elastix-laptop:~/projects_cg/match_delays/source$ for i in $(seq 1 2 20)
    > do
    > echo “Welcome $i times”
    > done
    Welcome 1 times
    Welcome 3 times
    Welcome 5 times
    Welcome 7 times
    Welcome 9 times
    Welcome 11 times
    Welcome 13 times
    Welcome 15 times
    Welcome 17 times
    Welcome 19 times
    dmitry@elastix-laptop:~/projects_cg/match_delays/source$ for i in $(seq 1 2 20); do; echo “Welcome $i times” ; done
    bash: syntax error near unexpected token `;’
    What am I missing here?

  47. @Dmitry
    You are missing : Reading The Fantastic Manual. :-)

    man bash
    for name [ in word ] ; do list ; done
    for (( expr1 ; expr2 ; expr3 )) ; do list ; done

    There should not be any “;” following the [do].

    for i in $(seq 1 2 20); do echo "Welcome $i times" ; done

    Good ol’ one: “When any thing goes wrong – (re) Read the manual”

  48. @Dmitry

    And, again, as stated many times up there, using [seq] is counter productive, because it requires a call to an external program, when you should Keep It Short and Simple, using only bash internals functions:

    for ((c=1; c<21; c+=2)); do echo "Welcome $c times" ; done

    (and I wonder why Vivek is sticking to that old solution which should be presented only for historical reasons when there was no way of using bash internals.
    By the way, this historical recall should be placed only at topic end, and not on top of the topic, which makes newbies sticking to the not-up-to-date technique ;-) )

  49. It is strangethat a do/done loop works if there is CR (end of line) after the do, but not if there is a semi-colon. I can see why this was confusing for you Dmitry, because it’s not logical. My guess is that the acceptance of CR after do was added because people wanted to lay out code this way, but the bash coders forgot to allow the semicolon alternative. As Philippe points out, if you follow the manual strictly, it works fine.

  50. @Philippe,

    I’ve just updated the faq and also deleted large data sample posted by dee user.

  51. @ Dominic

    Yes, it’s not quite intuitive, right. What you mean is the semicolon or the newline as list separator (list as the grammar construct defined in the manual, respectively by ISO9945). After a `do’, a list is expected, but a list isn’t introduced with a list separator. After a `do’, the shell awaits more input, just like after an opening quote character. In interactive mode, it also displays the continuation prompt `PS2′ instead of `PS1′ (it would display `PS1′ for list continuation).

    It’s not right that the Bash coders “forgot” it. Inspecting the grammar rules of POSIX XCU Sec. 2.10.2 doesn’t show a special rule here (it would have to be a special exceptional rule that extra allows a semicolon here).

  52. Me again.

    From all Bourne-like shells I just “tested”, only ZSH seems to support a semicolon as a start of a list (also in the case after the `do’). This is nice, but that’s all. It’s not a bug to not do so.

  53. @ TheBonsai

    Interesting. I accept that it is not a bug, but I still think it is confusing. It seems logical to us lesser mortals that in bash semicolon=newline, and in other situations I think this is true, but not here.
    # this works
    for (( c=1; c<=2; c++ )) do

    #any number of blank lines or even comments

    echo $c; done
    # so does this:
    for (( c=1; c<=2; c++ )) do echo $c; done
    # but this, where we substitute a semi-colon for the blank line(s) above, doesn't:
    for (( c=1; c<=2; c++ )) do; echo $c; done

  54. @Dominic
    You are missing the point.

    If you read our Unix pioneers, you will remember:

    – Rule of Optimization: Prototype before polishing. Get it working before you optimize it. [E. Raymond]

    What’s the point of spending hours to code on one line?
    – First, It does not give any optimization, it does not save any execution time. Your code will only be more difficult to read, check, debug.

    Defensive programming rules include this: Write one instruction per line.

    – Second, You still wanna code all on one line ?
    Big deal. The manual gave you the right way.
    So stick to it, or leave it, and skip to the next real problem, instead of wasting time and energy pointlessly, my dear Linux enthusiast.

  55. I have a comment to add about using the builtin for (( … )) syntax. I would agree the builtin method is cleaner, but from what I’ve noticed with other builtin functionality, I had to check the speed advantage for myself. I wrote the following files:

    for ((i=1;i<=1000000;i++))
    echo "Output $i"

    for i in $(seq 1 1000000)
    echo "Output $i"

    And here were the results that I got:
    time ./
    real 0m22.122s
    user 0m18.329s
    sys 0m3.166s

    time ./
    real 0m19.590s
    user 0m15.326s
    sys 0m2.503s

    The performance increase isn’t too significant, especially when you are probably going to be doing something a little more interesting inside of the for loop, but it does show that builtin commands are not necessarily faster.

    1. The reason why the external seq is faster, is because it is executed only once, and returns a huge splurb of space separated integers which need no further processing, apart from the for loop advancing to the next one for the variable substitution.

      The internal loop is a nice and clean/readable construct, but it has a lot of overhead. The check expression is re-evaluated on every iteration, and a variable on the interpreter’s heap gets incremented, possibly checked for overflow etc. etc.

      Note that the check expression cannot be simplified or internally optimised by the interpreter because the value may change inside the loop’s body (yes, there are cases where you’d want to do this, however rare and stupid they may seem), hence the variables are volatile and get re-evaluted.

      I.e. botom line, the internal one has more overhead, the “seq” version is equivalent to either having 1000000 integers inside the script (hard coded), or reading once from a text file with 1000000 integers with a cat. Point being that it gets executed only once and becomes static.

      OK, blah blah fishpaste, past my bed time :-)


  56. @Sean
    1) Again, when your only programming concern that last will be the optimization of your loops, you could invest time into such timings.
    This would be when there are no other bugs in your code, which I wish you to enjoy ASAP.

    2) But then you may find that the real problem/bottleneck is not a for loop.
    As Rob Pike said : “Measure. Do not tune for speed until your performance analysis tool tells you which part of the code overwhelms the rest.” [ ]

    3) I agree with you when you say that your code is not relevant as a timing of real-sized programs.

    4) Relating to your benchmark “builtin vs. external” commands
    test the builtin [ for i in {1..1000000} ]
    and you will see that it is very close to [for i in $(seq 1 1000000)]

  57. You are missing : Reading The Fantastic Manual. :-)

    A.. uh.. ?
    The lack of examples in the bash man page is the main reason to *avoid* man page. Everyone, knowns how to use them. Syntax is all good, only if you know the bash and UNIX in and out. This is the main reason why most people purchase bash and shell scripting books from O’reilly or Amazon. Rest of freeloaders depends upon Google and site like this to get information quickly. man pages are for gurus; for all new user examples are the best way to get started.

  58. @ 2012DD
    I agree – I use any of resource I need, man page, –help page, info page, web pages, books.
    The thing is: you should try to read man page once.
    And actually, if he did, he would have find the syntax.
    Vivek’s web site and contributions do not prevent you of reading the “fantastic” manual.

    And, as usual, the ones that issue the bitter critics will not move a finger to enhance the manual.

    You say man page lacks good examples?
    Did you ever try to contribute to any man pages ?
    Hope you did.

  59. The Bash manual page isn’t meant as tutorial. It’s a syntax, grammar and behaviour reference. However, it contains the knowledge to explain why a for loop using `seq’, one using brace expansion and one using builtin arithmetics have the performance relations they actually show when you execute them. The point is to make a relation between abstract descriptions and real execution behaviour. If such things really count, however, I suspect you code in the wrong language.

  60. @Bonsai
    > The Bash manual page isn’t meant as tutorial. It’s a syntax, grammar and behaviour reference.

    Actually, no. A man page can/should contain a EXAMPLE section.
    check :

    I am pretty happy when the Example section is relevant. And when you want some more, nothing prevents you to try to add new examples… Let’s contribute!

  61. @Philippe

    How many examples would you place there, to show the “common usage of Bash”? I agree that manpages usually should contain examples, but I think this would be too much. Huge manpages (huge because they describe a huge set of functionality) contain very small/no examples.

  62. @Philippe,

    I am learning Bash scripting and that is why I’m here and its wiki. Take a look at Solaris UNIX man page, most of them have good set of examples. Another candidate is FreeBSD, they also have good set of examples. Just discovered that our HP-UX came with printed “Posix Born Shell Scripting Manual”.

    >Did you ever try to contribute to any man pages ?

    No, I’m learning and if I *contribute* anything, I’m dam sure most shell scripting gurus will eat me alive, as I’m not experienced coder. Once I tried to help someone on and most of other people on list were so mean to my code that I almost stopped visiting *Beep* those bastards!

    Edited by admin

  63. @2012 Doom Day

    Contribution is more than just knowing code. Alone the fact that you write here is a contribution (to the community). Translations, documentation, searching bugs, helping others, sharing expiriences, …

  64. Quite right, Bonsai,
    and you can contribute to Wikibooks, Wikipedia, and Vivek’s Wiki using the books you own and all you have learnt, no one will ever prevent you of doing so, assuming you improve the content. Don’t be shy and be confident on your capacities.
    See, I wrote loads of questionable comments and Vivek has not banned me [yet] ;-).

  65. Do you know why this doesn’t output anything?

    for i in $(cat /$HOME/client_list.txt)
    echo $i > /home/$i_file.log

  66. @Chris C

    If you want a good explanation, first try to ask a good question and explain:
    1a) What you want to do with this program
    1b) what your program is supposed to do.

    Mainly, it will fail because there will not be variable expansion $i_file.log should be ${i}_file.log
    Read again about variable expansion.

    2a) in your ” cat “, there should not be a leading slash before $HOME (because $HOME contains a leading slash) – anyway this wont prevent it from working – but may come to bugs someday.

    2c) if a line in client_list.txt does contains spaces, what do you think this would do? Use quotes.

    2d) If it still fails, check your permissions to create and overwrite a file in “/home” directory

    2e) As said many times, you do not need to use ” for + cat ” to use the content of a file. Just use a while loop :

    while read i
    echo ${i}
    done < $HOME/client_list.txt

  67. Sir,
    This is a new post. From a file in unix server with a column of temperature, I want to extract a number if it goes greater than 100. Normally it will be in 60 – 80 range. Can u suggest a bash script?

  68. The {1..10} syntax is pretty usless as you can use a variable with it!

    echo {1..${limit}}

    You need to eval it to get it to work!

    eval "echo {1..${limit}}"
    1 2 3 4 5 6 7 8 9 10

    ‘seq’ is not avilable on ALL system (MacOSX for example)
    and BASH is not available on all systems either.

    You are better off either using the old while-expr method for computer compatiblity!

       limit=10; n=1;
       while [ $n -le 10 ]; do
         echo $n;
         n=`expr $n + 1`;

    Alternativally use a seq() function replacement…

     # seq_count 10
    seq_count() {
      i=1; while [ $i -le $1 ]; do echo $i; i=`expr $i + 1`; done
    # simple_seq 1 2 10
    simple_seq() {
      i=$1; while [ $i -le $3 ]; do echo $i; i=`expr $i + $2`; done
    seq_integer() {
        if [ "X$1" = "X-f" ]
        then format="$2"; shift; shift
        else format="%d"
        case $# in
        1) i=1 inc=1 end=$1 ;;
        2) i=$1 inc=1 end=$2 ;;
        *) i=$1 inc=$2 end=$3 ;;
        while [ $i -le $end ]; do
          printf "$format\n" $i;
          i=`expr $i + $inc`;

    Edited: by Admin – added code tags.

  69. @Anthony.
    Quite right – braces {start..end..step} might not be the best thing in bash.

    Nevertheless, I still stick to the old C-like synxtax in a for loop, which does accept variable arguments, such as:

    for (( x = $xstart; x <= $xend; x += $xstep)); do echo $x;done

    I don’t know much of this FOR loop syntax portability, functions you suggest may be the best thing to use for portability concern. (I have to read POSIX reference again :-) )

    1. The Bash C-style for loop was taken from KSH93, thus I guess it’s at least portable towards Korn and Z.

      The seq-function above could use i=$((i + inc)), if only POSIX matters. expr is obsolete for those things, even in POSIX.

      1. TheBonsai wrote…. “The seq-function above could use i=$((i + inc)), if only POSIX matters. expr is obsolete for those things, even in POSIX.”

        I am not certain it is in Posix. It was NOT part of the original Bourne Shell, and on some machines, I deal with Bourne Shell. Not Ksh, Bash, or anything else.

        Bourne Shell syntax works everywhere! But as ‘expr’ is a builtin in more modern shells, then it is not a big loss or slow down.

        This is especially important if writing a replacement command, such as for “seq” where you want your “just-paste-it-in” function to work as widely as possible.

        I have been shell programming pretty well all the time since 1988, so I know what I am talking about! Believe me.

        MacOSX has in this regard been the worse, and a very big backward step in UNIX compatibility. 2 year after it came out, its shell still did not even understand most of the normal ‘test’ functions. A major pain to write shells scripts that need to also work on this system.

        1. Yea, the question was if it’s POSIX, not if it’s 100% portable (which is a difference). The POSIX base more or less is a subset of the Korn features (88, 93), pure Bourne is something “else”, I know. Real portability, which means a program can go wherever UNIX went, only in C ;)

  70. That {1 .. N} syntax doesn’t work with current Linux bash.

    $for r in {1 .. 15}; do echo $r; done

    1. Yes, it does works, you need bash version 3.0 or up. Just tested with “GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)”. You need to remove white space between 1 and 15, try:

      for r in {1..15}; do echo $r; done
  71. I gave some ‘seq’ alternatives, some simple, some more complex, mostly using shell built-ins only, depending on you needs.

    At the very start of the comments “jot” was mentioned as an alternative, though it does not appear to be as wide spread as “seq”. Anyone know if it is on the ‘limited shell suport’ MacOSX?

    There are also however some other — off the wall — methods of generating a list of number, or just a list for looping ‘N’ times. One of the weirdest ones I came across was using /dev/zero and “dd”!

    dd 2>/dev/null if=/dev/zero bs=10 count=1 | tr \ \12 | cat -n | tr -d ‘\40\11′

    This gets ’10’ null characters, converts them to line feeds, uses cat to convert them to numbers, and just to clean up, you can optionally delete the tabs and spaces.

    As I said real odd ball.

    Like I often say…
    There are lots of ways to skin a cat, and what method you use depends
    on what you want that skin for, and how messy you like the results!

    1. Yes,
      but the code we see won’t work.
      I think it needs some syntax enhancement in your first [ tr ], such as:

      dd 2>/dev/null if=/dev/zero bs=10 count=1 | tr '00' '12' | cat -n | tr -d '\40\11'

  72. Hi guys Ive been reading this thread cos i need some advice on a script. I need to rename some (lots) of files in a directory. they are named..
    file_name.003.01… etc

    How can I change the names of the files to remove the ‘.01’ at the end of each filename? Im useing Ubuntu Lynx…. Ive been playing with a few examples from this thread, but cant seem to make it work. Any help is appreciated

      1. In general, if you want to add a suffix to your files, do this (.txt in this example):

        file1 file2 file3

        for i in *; do mv $i $i.txt; done

        file1.txt file2.txt file3.txt

        If you want to take it back off (.txt in this example again)

        for i in *.txt; do mv $i ${i%.*}; done

        file1 file2 file3

        1. Of course, if you want to worry about files with spaces in (and other things?), put quote around the arguments to mv, as in the gp.

          1. Note that is you want to append a string that does not start with a ‘.’ (for example the string “_info.txt”) then you need to delimit the variable name….

            for i in *; do mv “$i” “${i}_info.txt”; done

  73. Hi,
    I have two files that contain diffrent columns. Both files have matching one column but raw oder is different. I want to combine these two files as below.
    File 1: file 2
    x 2 7 123 r 3 5 9
    y 3 -8 124 y 4 6 20
    z 4 -2 34 q 3 5 70
    q 5 -9 5 z 5 4 10
    r 6 1 6 x 50 3 40

    I want to combine each raws considering the common values in the first column
    x 2 7 123 50 3 40
    q 5 -9 5 3 5 70

    I would be grateful if you could help me with this problem.

    Thank you.

    1. Does it really matters?

      Bash runs on both certified UNIX and UNIX like (*BSD & various Linux distros) operating systems.


      1. @Vivek

        Quite right – who cares ? Nobody.

        I just hope that those guys [borleand] would have better working and helping for FOSS community instead of posting those high-quality ( ;-P ) comments.

        Thanks for such a great web site, Vivek.

      2. Of bigger concern between UNIX, GNU, GPL, Linux, Solaris, MacOSX, or whatever else, is what extra support programs are also available.

        I use to use ‘seq’ all the time in shell loops. I don’t any more because ‘seq’ is not available on MacOSX. In fact a lot of simple and what I would have though universal support programs are not available on MacOSX.

  74. how do i run 100 iteration using bash shell script.. i want to know how long will it take to execute one command(start and end time). I want to keep track which iteration is currently running. i want to log each iteration. I have one automated script i need to run and log it.

    for i in 1 2 3

    But i want to know how long it takes to complete one iteration. and writes a log… help me please

    1. Hi naveen,

      1) You should at least read thoroughly this topic. Iteration can be done with this syntax

      for x in {start..end}

      Look above for explanation.

      2) You could use [date] command and output it to a file
      – as first command of the iteration (echoing something to mark start)
      – as last command of the iteration (echoing something to mark end)

      3) Log
      What kind of log? To know what? What for?

      1. i want to write a output in text file(.txt)(log). I want to have a report saying that test ran for 100 iteration. My question i have one automated script that run 100 test file. i want to know how long it takes to complete the one iteration. And i want to keep a copy of the test result in .txt file.

        i know this is to log for one iteration…….
        command > log.txt

        But when you are running iteration for 100. How wil you log it??. this is my question. Just one automated script (command) but there will be 100 test results for 100 iteration. How wil you log all this???

        1. You really seem to be a beginner in shell – the best way to start would be to study Vivek’s Wiki first.

          To answer you iteration question:
          1) First write the appropriate [for] loop that calls you commands and show us your code. All the information you need is located above in this page. If you cannot write this loop, you’d better learn [bash] on Vivek’s Wiki and come back when you can write a [for] loop with 100 iteration. Good luck! :)

  75. The problem with this is that csv files can contain quoted strings. which makes just comma separation usless.. For example

    1. In that case use ” as delimiter. This can be done with sed or cut or any other shell built-in or utility:

      echo '"ab1,pp1","ab1","pp1"' | cut -d '"' -f2
      echo '"ab1,pp1","ab1","pp1"' | cut -d '"' -f4
      echo '"ab1,pp1","ab1","pp1"' | cut -d '"' -f6

      for loop can be used:

      for i in {2..6..2}; do echo '"ab1,pp1","ab1","pp1"' | cut -d '"' -f${i}; done

      You can skip for and use sed too.


  76. It isn’t quite that simple. But then any CSV that is more complex is getting a bit beyond simple shell parsing.

    1. Here is another way to do it,
      – in a generic way, that is without having to know number of internal fields,
      – without IFS manipulation
      – without external function (only builtin):

      Let’s say your CSV file contains 3 fields per record (per line):
      “aaa bbb ccc”,”ddd eee fff”,”ggg hhh iii”,”jjj kkk lll”
      “mmm nnn ooo”,”ppp qqq rrr”,”sss ttt uuu”,”vvv www xxx”
      “yyy zzz 111″,”222 333 444″,”555 666 777″,”888 999 000”

      To break it in a one-liner, try:

      while read; do record=${REPLY}; echo ${record}|while read -d ","; do echo ${REPLY}; done; done<data

      The same code in a script is:

      while read
      echo ${record}|while read -d ,
      echo ${REPLY}
      1. And if you want to get rid of double-quotes, use:

        one-liner code:
        while read; do record=${REPLY}; echo ${record}|while read -d ","; do field="${REPLY#\"}"; field="${field%\"}"; echo ${field}; done; done<data

        script code, added of some text to better see record and field breakdown:

        while read
        echo "New record"
        echo ${record}|while read -d ,
        echo "Field is :${field}:"

        Does it work with your data?

        — PP

        1. Of course, all the above code was assuming that your CSV file is named “data”.

          If you want to use anyname with the script, replace:




          And then use your script file (named for instance “myScript”) with standard input redirection:

          myScript < anyFileNameYouWant


          1. well no there is a bug, last field of each record is not read – it needs a workout and may be IFS modification ! After all that’s what it was built for… :O)

  77. Another bug is the inner loop is a pipeline, so you can’t assign variables for use later in the script. but you can use ‘<<<' to break the pipeline and avoid the echo.

    But this does not help when you have commas within the quotes! Which is why you needed quotes in the first place.

    In any case It is a little off topic. Perhaps a new thread for reading CVS files in shell should be created.

    1. Anthony,
      Would you try this one-liner script on your CSV file?

      This one-liner assumes that CSV file named [data] has __every__ field double-quoted.

      while read; do r="${REPLY#\"}";echo "${r//\",\"/\"}"|while read -d \";do echo "Field is :${REPLY}:";done;done<data

      Here is the same code, but for a script file, not a one-liner tweak.

      # script
      # 1) Usage
      # This script reads from standard input
      # any CSV with double-quoted data fields
      # and breaks down each field on standard output
      # 2) Within each record (line), _every_ field MUST:
      # - Be surrounded by double quotes,
      # - and be separated from preceeding field by a comma
      # (not the first field of course, no comma before the first field)
      while read
      echo "New record" # this is not mandatory-just for explanation
      # store REPLY and remove opening double quote
      # replace every "," by a single double quote
      echo ${record}|while read -d \"
      # store REPLY into variable "field"
      echo "Field is :${field}:" # just for explanation

      This script named here [] must be used so: < my-cvs-file-with-doublequotes

      1. @Anthony,

        By the way, using [REPLY] in the outer loop _and_ the inner loop is not a bug.
        As long as you know what you do, this is not problem, you just have to store [REPLY] value conveniently, as this script shows.


  78. Thanks for writing this article Vivek – it is very useful. In particular, I didn’t know about bash’s built-in ‘help’ command and was getting frustrated with the lack of detail in ‘man for’

  79. Hi i need help in sorting some of the raw data actually on the unix machine.

    The raw data is some thing like this:


    So now it has to check for the time stamp T0145 is the timestamp in the below code :

    TOP;0004806;T0145;40,3;38,8;1,5;427904;32648;8;0;1000;62;0;java< ?pre>
    now this has to be mapped with the same time stamp in ZZZZ.......Which is some thing like this from above.

    so finally the output should look like this for every occurance the T value:

    TOP;0004806;T0145;40,3;38,8;1,5;427904;32648;8;0;1000;62;0;java ZZZZ;T0145;02:24:04;18-NOV-2010,
      1. Hi Vivek,

        Thanks for your reply,

        but this is just not only sorting but recording on of the value in the above code and then match this with other lines in the code .then display both together.

  80. @Sammeta,

    Just trying to help, and not to being rude or anything:
    We are not willing to do your work,
    anyone would help people who really try to help themselves first,
    which you may have been trying already, I suppose.

    So, would you either submit a first version of your code,
    or at least a main Algorithm you could think of?

    You may want to read first [awk] or [join] unix utilities man pages that you could find anywhere.


  81. One good reason to use seq instead of the {start..end..increment} idiom:

    #! /bin/bash

    echo — use seq —
    for x in $(seq $START $INCR $END)
    echo $x
    echo — bash idiom —
    for x in {$START..$END..$INCR}
    echo $x

    — use seq —
    — bash idiom —

    1. @Bonsai:

      The example you gave is weird regarding shell variable usage: It works, but I thought it should not work!
      Within the for (( )) instruction, you omitted “$” sign to allow variable expansion, but it works! That looks very strange to me.

      I wrote in an example previously (look above):

      for (( x = $xstart; x <= $xend; x += $xstep)); do echo $x;done

      In your example, you wrote [for(( ))] without “$” :

      for ((x = START; x <= END; x += INCR))

      I am astonished that for(()) works both with and without “$” for variable expansion!

      for (( x = xstart; x <= xend; x += xstep)); do echo $x;done

      Does anyone know why?


      1. I think it is ksh93 compatibility feature; so “START / END / INCR” will work with no “$”. See ksh93 man page.

        1. Sorry, I cannot find any evidence of such syntax on khs93 man page – and I use Bash shell.
          On what URI + what chapter do you think there is an explanation of such behavior?

          1. It’s not KSH (or at least not KSH-unique). It’s how arithmetic environments (of any kind) in Bash work.

            Also it’s related to what POSIX specifies for the environment inside arithmetic expansion (the only a. environment POSIX knows):

            If the shell variable x contains a value that forms a valid integer constant, then the arithmetic expansions “$((x))” and “$(($x))” shall return the same value.

  82. I might be going out on a limb due to a bad case of TL;DR, but I noticed the seq warning.

    Correct me if I’m wrong but using for instance

    for i in $(seq -w 1 1 20); do echo $i; done

    is in my oppinion quite an useful way of using seq in bash, at least when you want leading zeros before 1-9 and not from 10<

    Or is there a better way of doing this all "bashy"? ;)


    1. +1 for printf due to portability, but you can use bashy .. syntax too

      for i in {01..20}; do echo "$i"; done
      1. Well, it isn’t portable per se, it makes it portable to pre-4 Bash versions.

        I think a more or less “portable” (in terms of POSIX, at least) code would be

        while [ "$((i >= 20))" -eq 0 ]; do
          printf "%02d\n" "$i"
  83. Hi

    Can anybody help for this.

    I have two text file viz gem1.txt and gem2.txt

    EX. Content of gem1.txt

    activerecord (2.3.5, 2.2.2)
    activerecord-oracle_enhanced-adapter (1.1.9)
    activerecord-sqlserver-adapter (2.3.4)
    activeresource (2.3.5, 2.2.2)

    Now i have to put all the data of these two .txt file on gem.csv. column wise.

  84. Hi
    can some one help in ceating a loop

    the problem is
    i have 3 folder and name of the folder changes.
    i want a loop which enter these folder one by one echo the files inside the folder

  85. I didn’t see this in the article or any of the comments so I thought I’d share. While this is a contrived example, I find that nesting two groups can help squeeze a two-liner (once for each range) into a one-liner:

    for num in {{1..10},{15..20}};do echo $num;done

    Great reference article!

  86. Hi guys.
    I was wondering whether it is possible to run a for loop through rsh (now before you start protesting that I use rsh instead of ssh; I know that it is extremely insecure. Thanks.) like this:?
    rsh -l dev 192.168.x.x “for file in /PackSun/distills/*; do if [[ ${file} =~ “3\.6\.76″ ]]; echo $file; fi; done”

    Because when I type this it doesn’t work, $file is always the same file and it comes from the pwd on the local machine, not on the remote.
    Thanks in advance.

    1. damn, sorry, didn’t close the code tag:

      rsh -l dev 192.168.x.x “for file in /PackSun/distills/*; do if [[ ${file} =~ “3\.6\.76” ]]; echo $file; fi; done”

  87. First — you should not use rsh. It is an old and insecure network protocol. ssh is its replacement.

    As for your question YES it is posible, Wrap it in a ‘shell’ command.

    rsh -l dev 192.168.x.x ‘sh -c ‘\”for file in /PackSun/distills/*; do if [[ ${file} =~ “3\.6\.76″ ]]; echo $file; fi; done’\’

    Watch the quoting as you want to use single quotes for both the outside and inside commands. If you use ” quotes you will need to escape the $ characters instead! And that may in turn lead to escapes of escapes (not pretty).

    I have actually done this to execute a very very large shell script on a remote server.
    However I don’t recommend it for things beyond what you have. If need be copy (update) a shell script on the remote server (scp, rsync, etc) then execute that. It works better, you do not have constant quote handling problems, and not command line length limits.

    I do this for a incremental home backup program (written for my own use, using rsync hardlinks backup directories), that can backup to a remote account. You are free to download and look at the copy/execute remote script that the main script performs for its sub-scripts (like backup cycle rolls).

    1. Anthony,

      You forgot to include URI for you script, your > a < tag is empty.

  88. PS: the reason for the ‘sh -c …’ is beause you may nto have control of the login shell of the remote account. If you did not have it and the remote accoutn used tcsh or zsh, you will have problems.

    PPS; you are missing a then. I tested it with this ssh version (a shell-builtin only ‘ls’ of the remote account).
    ssh remote_machine ‘sh -c ‘\”for file in *; do echo $file; done’\’

  89. Hello

    I am using bash V4+ and this loop works fine:

    for i in {1..10}; do echo $i; done

    But when I put variables in the loop

    max=10; for i in {1..$max}; do echo $i; done

    I see one line as output:


    Could anybody explain me what’s wrong ?

    Cheers. Sanya

    1. Hello Sanya,

      That would be because brace expansion does not support variables. I have to check this.
      Anyway, Keep It Short and Simple: (KISS) here is a simple solution I already gave above:

      for (( x = $xstart; x <= $xend; x += $xstep)); do echo $x;done

      Actually, POSIX compliance allows to forget $ in for quotes, as said before, you could also write:

      for (( x = xstart; x <= xend; x += xstep)); do echo $x;done

      1. Sanya,

        Actually brace expansion happens __before__ $ parameter exapansion, so you cannot use it this way.

        Nevertheless, you could overcome this this way:

        max=10; for i in $(eval echo {1..$max}); do echo $i; done

        1. Hello, Philippe

          Thanks for your suggestions
          You basically confirmed my findings, that bash constructions are not as simple as zsh ones.
          But since I don’t care about POSIX compliance, and want to keep my scripts “readable” for less experienced people, I would prefer to stick to zsh where my simple for-loop works

          Cheers, Sanya

          1. Sanya,

            First, you got it wrong: solutions I gave are not related to POSIX, I just pointed out that POSIX allows not to use $ in for (( )), which is just a little bit more readable – sort of.

            Second, why do you see this less readable than your [zsh] [for loop]?

            for (( x = start; x <= end; x += step)) do
            echo "Loop number ${x}"

            It is clear that it is a loop, loop increments and limits are clear.

            IMNSHO, if anyone cannot read this right, he should not be allowed to code. :-D


  90. If you are going to do… $(eval echo {1..$max});
    You may as well use “seq” or one of the many other forms.
    See all the other comments on doing for loops.

  91. I am trying to use the variable I set in the for line on to set another variable with a different extension. Couldn’t get this to work and couldnt find it anywhere on the web… Can someone help.


    FILE_TOKEN=`cat /tmp/All_Tokens.txt`
    for token in $FILE_TOKEN
    A1_$token=`grep $A1_token /file/path/file.txt | cut -d ":" -f2`

    my goal is to take the values from the ALL Tokens file and set a new variable with A1_ infront of it… This tells be that A1_ is not a command…

    Edited by admin: Added pre taga

  92. could you please help me in write a bash script do the following:
    1- read an input file contains IP Port
    2- check the connectivity for every IP and its Port via telnet
    3- if the telnet not connected send email to alert me.

    thanks in advance
    atef fawzy

      1. dear Philippe Petrinko
        thank you a lot for you value site
        the below is my script and i don’t know what is the wrong?can you help me

        for ip in $(cat iplist);

        # check for open ports #

        connTest=`echo ” ” | telnet $ip`

        if [ “`echo $ip | awk ‘{ print $3 }` = “Connected” ]
        echo “$ip is up”


        #echo “$connTest port is down”
        echo $ip is down


      1. please tell me what is the wrong?

        for ip in $(cat iplist);

        # check for open ports #

        connTest=`echo ” ” | telnet $ip`

        if [ “`echo $ip | awk ‘{ print $3 }` = “Connected” ]

        echo “$ip is up”


        echo $ip is down


  93. dear Philippe Petrinko
    thank you a lot for your value site
    the below is my script and i don’t know what is the wrong? can you hep me please

    for ip in $(cat iplist);

    # check for open ports #

    connTest=`echo ” ” | telnet $ip`

    #if [ “`echo $connTest | awk ‘{ print $3 }` ” = “Connected” ]
    if [ “`echo $ip | awk ‘{ print $3 }` = “Connected” ]

    #echo “$connTest port is up”
    echo “$ip is up”


    #echo “$connTest port is down”
    echo “$ip is down”


    1. Your request is off-topic.
      Your [for] loop works fine, so this is not a point to be discussed here,

      Your [if] test is buggy – not to be discussed here but there

  94. #!/bin/sh
    How can I do this using infinite loops?

    for disk in $(ls /dev/disk/by-id/ | grep -v part | grep scsi-35000I);
    echo $disk && \
    dd if=/dev/zero of=/dev/disk/by-id/$disk bs=8M oflag=direct count=250 && \
    dd if=/dev/disk/by-id/$disk of=/dev/null bs=8M count=250;
  95. hey,

    Thanks a lot. Example are in the way they should be…some of the examples are very new to me. It has increased by knowledge.
    keep posting these things and let us increase our knowledge.

  96. Hi,

    I have to read a file line by line and do the iteration and apply the condition parameter for each input . Please share your ideas

  97. Hi, I’m a beginner and I was writing a very simple script :

    echo “Enter username:”
    read username
    if [ “$username” = “newbay” ]
    echo “Username correct,”
    echo “Incorrect username, try again:”


    echo “Now enter password:”
    read password
    if [ “$password” = “welcome” ]
    echo “You are now logged in.”
    echo “Sorry, incorrect password. Please try again.”

    and I was wondering how to loop the incorrect username try again part ? Can anyone help me, if they understand my awful script ..

  98. Very handful tutorial! : ) But i’ve got a problem on my Ubuntu linux box, a strange problem.. Executing the example code

    for i in {1..5}
    echo “Welcome $i times”

    i get as output Welcome {1..5} times. So the bash doesn’t understand the range {1..5}…Have any idea? My bash version is GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu). Thank u : )

    1. This code has to be written into a text file, which must be made executable.
      Did you do that?

    2. GNU/Bash v4.2.8 does supports {1..5} syntax. What is the output of the following commands?

      ls -l /bin/bash
      /bin/bash --version
  99. Great weblog right here! Additionally your web site quite a bit up very fast! What host are you the usage of? Can I get your associate hyperlink for your host? I want my website loaded up as fast as yours lol

  100. Hi all… I have a question about using for loops. Basically, I have a file containing a list of protein ID numbers. what I want to do is create a for loop that can go into this file, grab each protein ID number, and then search the NCBI database to get the fasta sequence of each protein… and then create another file containing all of the fasta sequences….

    In general, my problem is that I can’t figure out how to get the protein ID numbers from the output file (ex. $1 in file1) into the for loop script.

    This is what I have so far:

    for gi in file1
    fastacmd -d /data/nr -s gi

    but I need to specify that the gi (protein ID number) is the first column ($1) of file1.

    Does this make sense?

  101. Hi…
    Thanks for the article, it is helpful really.

    I want to know one thing.
    Let’s say there is file with content

    how can I use for loop and print these numbers?
    If you are understanding what I am trying to tell. I want to use the contents of these files and stored in the variable.

  102. Hello!
    i am beginner and i have to make a bash script in which i have to show the number of lines that were added by the analized commits(in git).can you help me?Thanks.

      1. “infamous” was a private joke-understatement
        Sorry, no offense intended!
        Vivek website roxxxxs !

        — Philippe

        1. Thanks for your quick answer.
          I have another problem.i have to use this script by using git.can you explain me or give some advices how can i use it?thank you very much.

        2. I have this code that shows the first column which represents the number of insertions of a commit and calculate the sum of all numbers of each line.
          if [ $# -eq 2 ]; then
          if [ $2 = “added_lines” ]; then
          git log –pretty=tformat: –numstat | tr -s “\n” | cut -f 1 > $tmpfile
          for i in $(cat $tmpfile); do
          sum=$(($sum + $i))
          echo “$sum”
          rm $tmpfile
          But with ‘cut -f 1’ it takes too much time to calculate and to show the result.Can you help me saying how can i do this with ‘awk’ or ‘gawk’?
          Please help me.Thanks

  103. Hi Vivek,
    i am trying to run the below code as in server as $ sh ./


    for i in 1 2 4
    echo “Welcome $i times”

    It fails saying line 3: syntax error near unexpected token `do line 3: `do

    is that something i need to check which version is the sh and bash used in the server. how to check that.
    it is a pretty simple code and it is not workign.
    i am running this script in Linux server 64 bit server.

    1. What’s the easiest way to have a loop from N to N, when the numbers are dictated by variables?

      Something like what one could/would expect from “for i in {$a..$b}”, but something that actually works of course.

      What I do is:

      a=0 ; b=5 ; until ((a==b)) ; do echo $a ; a=$(($a+1)) ; done

      But it seems kind of dumb, somewhat like echoing a huge set of spaces instead of “clear”, to clear the screen. But perhaps it’s the only way to do it. And it works anyway, so perhaps it’s good enough and worthy as an addition rather than a question.

      Funnily enough you can make an alphabetic countdown with “for i in {a..z}”

  104. Hi,
    I have this code in NetBeens:
    using namespace std;
    int main() {
    int a,i,j;
    cout << "inter your number of row " <>a;
    for (i=0; i<=a; i++)
    for(j=0; j<=i; j++)
    cout << "*";
    cout << '\n';

    return 0;

    but I want to run it in ubuntu so at first I made a " nano" aftaer that I wrote this code in it:
    echo “Enter your number of rows”
    Read a
    for((i=0; i<=a; i++))
    for((j=0; j<=i; j++))
    echo “*”
    echo –n
    exit 0

    after that " chmod +x", but my code didn't run, my code in ubuntu has problem,
    I don't know how can I solve it, Thanks a lot if you answer my question,

  105. hi, am having problem to write a program using For statement. Please help if you know something about using For statement

  106. Vivek,
    It seems that WordPress was hungry, it has eaten your text ;-)

    [for] code sample is broken after sentence: “A representative three-expression example in bash as follows”


  107. i try the infinite variable, but it tells me that:

    Syntax error: Bad for loop variable


  108. Hello,
    Nice post.

    Please help me in below code. we are getting syntax error. Please resolve the syntax issue.

    if [ $1 ] ; then
        if -d ${_LIB} ; then
            for jar in (cd ${_LIB}; ls *.jar) 
                EXISTS=echo ${CP} | grep "/${jar}" 
                if [ "${_EXISTS}" != "" ]; then
                    logger "WARN: Classpath will contain multiple files named ${jar}" 
        cd $1 


    syntax error near unexpected token `('
    line 15: `for jar in (cd ${_LIB}; ls *.jar)'


    1. You are a ‘help troll’.. You posted in the wrong section.
      you would have been better of starting a new topic.

  109. try with

    if [ $1 ] ; then
    if -d ${_LIB} ; then
    for jar in $(cd ${_LIB}; ls *.jar)
    EXISTS=$(echo ${CP} | grep “/${jar}”)
    if [ “${_EXISTS}” != “” ]; then
    logger “WARN: Classpath will contain multiple files named ${jar}”
    cd $1

  110. hi if I use it with a imput variable like:

    echo “enter the numbers of repetitions :”
    read variable

    tthe variable represent the number example 15 times to repeat !!!???/

    how could it be ???

  111. nice and clear thx a lot.
    What I am dreaming of is this clear kind of webpage in which each box has a tab in which you can select any language…. I’m sure it exists already but where ???

    and I mean it needs to be clear and simple !!

  112. hi i have a problem i want to write a shell script for siesta.
    i want to make diffrent directories and want to change lattice constants n then run it with siesta n want to chck total energy
    cn u tell m how cn i do this

      1. I want to write a shell script
        for diffrent values of lattice constants .
        If i do manually i first make a directorie i.e for 3.80 then i copy *.psf and fdf file there open the fdf file and change lattice constant to 3.80 and the execute file with siesta and note the total energy.
        n again i repeat the same process for let say 3.90, etc.
        now i want to write a code in shell so that i dnt need to make directory every time and change lattice constant.
        I want to use looping for this purpose……..but how??
        anybody have the little code for this?or anything?

  113. #!/bin/bash
    # changing lattice constants
    for 3.80 3.90 in $inp3.70.fdf
    echo 3.80 3.90
    mv inp3.70.fdf inp*.fdf.old
    mv *.psf *.psf.old
    sed ‘s/inp*.fdf/&, lattice constant/’ inp*.fdf
    # Now comes the commands to be executed
    ~/code/sanabin/siesta 3.70.out &
    as i write this but not working…….. ../

  114. nice you used “c++” in “A representative three-expression example in bash” but that is a programing language.

    1. @matthias :

      What’s your point?

      Is it a humorous play of words on “C++” language and increment of c variable in this script?

      In that case it would have been more convenient to append a smiling smiley, because otherwise, there is no coding issue in “A representative three-expression example in bash”.

      :-/ wondering…

      — Philippe

  115. Hi.. check it

    for (( c=1; c<=5; c++ ))
       echo "Welcome $c times"

    why came this error in ubuntu please tell me
    reply please
    ./for3: 1: ./for3: Syntax error: Bad for loop variable

    1. Hi,

      It works for BASH only.

      Check you have given the right interpretor at the top of the script.
      You can give it by placing

      #! /bin/bash at the top.


  116. Can anyone advise how to write a for loop statement to run commands from a .sh when an event comes up like users uploading new files to the server.

    My issue is that I am using a soft link to mirror an external disk drive in the .www/ and the soft link never updates when a new content is added to the drive within a session.

  117. Hi. Nice overview of for loops. I have a question, however:
    Using the old “seq” command, one could perform zero padding via “seq -f “%05g” 1 100″. How would this work with the {1..100} (or other) syntax?

    1. Hi Dai,



      for x in {0001..10} ; do echo “padding :$x:”; done

      Actually it works specifying padding on first argument (here 0001). No need to specify padding on second argument, but it will either work.

      for x in {0001..0010} ; do echo “padding :$x:”; done

      3) but beware: you can specify different padding on both arguments, but only the _longuest_ will be used !

      so this will use 6 digits padding, not 3 !

      for x in {001..000010} ; do echo “padding :$x:”; done

      ok ?

      Vivek, would improve this topic on for loop adding this information on padding? It seems to be useful and at least informative and relevant to this topic.

      — Philippe

  118. You cannot completely abandon seq for the new bash syntax. Apparently, all variables in bash are integers. I tried using the new syntax with negative and float numbers with disastrous results. Seq, in turn, does it wonderfully.

  119. Ooops, forget it, someone else said the same thing. Didn’t see that, I’m not sure why…

  120. Useful information! thank you!

    Can somebody explain me what this loop “for i in circles[0,:]:” means?

  121. HI:

    I use seq when I need variables in the loop limits, because as far as I remember

    for y in {$x1..$x2}… is not allowed.


  122. Hi i would like to know how to loop numbers using terminal but i tant seem to find how to so it would be gréât if one of you can help me

  123. #!/bin/bash
    for ((i=1;i<=100;i++));
    echo $i
    while excute the above program the below errors comes please check revert

  124. hi
    i would like to know how i write a shell script to search a 100 user home directory by certain file
    can you help me?

  125. Hey I hope you can help me here. I’m stuck.

    down vote

    Trying to bind 20 x /24 subnets in Ubuntu 14 Server but I’m stuck here. These are diversified /24 subnets.

    I tried the below method via script called

    for i in $(seq 3 254); do echo “auto em1:$i
    iface em1:$i inet static
    ” >> virthosts; done

    But, I noticed that this happened now:

    Each /24 subnet it outputted started off at #1 again instead of 255, 256, 257, etc all the way through consecutively for all 20 x /24 subnets

    auto em1:1
    auto em1:254

    Then again…

    auto em1:1

    How can I properly change this so it does reset at #1? em1 is the NIC Ethernet port (primary) and only port used too.

    I also want to make sure these are permanent, they stick after a server reboot.

  126. Hello There,

    I want to make one scipt which shall change multiple file name as per below example

    1) if file name contains *abc* then file name should be change to *xyz*
    2) if file name contains *def* then file name should be change to *iop*
    3) if file name contains *(any file name except above name) then file name should be change to *qwe*

    Kindly help me to make the script as mentioned above requirement


  127. These are great examples, although I am hoping someone can help me with an easy one.

    Below is a simple script, but I would like for it to ask for another hash or give the option to break instead of just breaking.

    echo "Hello, my name is" $TOKEN
    echo -n "Now, enter a hash: "
    read HASH
    curl -H "X-TOKEN-KEY: $TOKEN" https://$URL/hashcheck/check?hash=$HASH
  128. I would like to know how can i use for loop to pick up first few files from directory do something and then run with next batch.
    Currently, I am able to for each file it takes one file at a time however i want to launch a command against 4 files at a time and then move on to next set in same directory.

    Still, have a question? Get help on our forum!