Bash For Loop Examples

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.

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


Bash for loop examples for Macos/Linux and Unix
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 to create standard bash for Loop (outdated method)

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/step (EXP3).

for (( EXP1; EXP2; EXP3 ))
## The C-style Bash for loop ##
for (( initializer; condition; step ))

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

For loop with array elements

In this example, we use the for loop to iterate over an array of elements defined as follows:

DB_AWS_ZONE=('us-east-2a' 'us-west-1a' 'eu-central-1a')
for zone in "${DB_AWS_ZONE[@]}"
  echo "Creating rds (DB) server in $zone, please wait ..."
  aws rds create-db-instance \
  --availability-zone "$zone"
  --allocated-storage 20 --db-instance-class db.m1.small \
  --db-instance-identifier test-instance \
  --engine mariadb \
  --master-username my_user_name \
  --master-user-password my_password_here

Loop with a shell variable

Sometimes we store important data in the shell variable, and we can use for a loop as follows to read the data:

for e in $_admin_ip
   ufw allow from "${e%%|*}" to any port 22 proto tcp comment 'Open SSH port for ${e##*|}'

Loop with a number

We can specify a range in loops as follows:

for i in {START..END}
## step value ##
for i in {START..END..STEP}
## example: ping cbz01, cbz02, cbz03, and cbz04 using a loop ##
for i in 0{1..4}
    ping -c 1 -q "$h" &>/dev/null 
    if [ $? -eq 0 ]
        echo "server $h alive" 
        echo "server $h dead or can not ping."

Loop with strings

Say we have a variable named PKGS, and we need to loop through a list of strings to install those packages:

PKGS="php7-openssl-7.3.19-r0  php7-common-7.3.19-r0  php7-fpm-7.3.19-r0  php7-opcache-7.3.19-r0 php7-7.3.19-r0"
for p in $PKGS
   echo "Installing $p package"
   sudo apk add "$p"

Command substitution

Command substitution means run a shell command and store its output to a variable. For example:

echo "Server uptime is $up"

The for Loop argument list also workes command substitution as follows:

for var in $(command)
  print "$var"
## example ##
for f in $(ls /nas/*.pdf)
  print "File $f"

Command-line arguments

A command line argument is nothing but an argument sent to a program being called. A program can take any number of command line arguments. For example, we are going to use the grep command to search for user names in the /etc/passwd file:
$ grep 'vivek' /etc/passwd
grep is the name of an actual command and shell executed this command when you type command at shell prompt. The first word on the command line is:

  • grep – name of the command to be executed.
  • Everything else on command line is taken as arguments to this command.

The for Loop argument list also accpents Command-line arguments/paramenters as follows:

## $@ expands to the positional parameters, starting from one.  ##
for i in $@
    echo "Script arg is $i"

You run it as follows:
./script one foo bar

Putting all together

Bash for loop is useful for automating repetitive tasks in IT. Let us see how to run a simple command (such as uptime) on multiple Linux or Unix servers:

for s in server1 server2 server3
    ssh vivek@${s} "uptime"

OR combine the echo command along with command substitution as follows:

for s in server1 server2 server3
    echo "Server ${s}: $(ssh vivek@${s} uptime)"

Sample outputs:

Server server1:  09:34:46 up 12 days, 21:57,  0 users,  load average: 0.08, 0.09, 0.09
Server server2:  09:34:50 up 17 days,  2:30,  0 users,  load average: 0.03, 0.03, 0.00
Server server3:  09:34:53 up 17 days,  2:31,  0 users,  load average: 0.04, 0.04, 0.00

In this standard bash for loop example we are going to update all CentOS/RHEL based servers using the yum command or apt command/apt-get command in case we have a Debian/Ubuntu based servers:

## CENTOS/RHEL example (for fedora replace yum with dnf) ##
for s in server0{1..8}
    echo "*** Patching and updating ${s} ***"
    ssh root@${s} -- "yum -y update"

Here is simple but useful shell script example:

#!/usr/bin/env bash
# Purpose: Update all my Linode servers powered by Debian/Ubuntu Linux
# Author: Vivek Gite under GPL v2.x+
# ----------------------------------------
for s in ln.cbz0{1..5}
   echo "Updating and patching $s, please wait..." | tee -a "${log}"
   ssh root@${s} -- apt-get -q -y update >/dev/null
   ssh root@${s} -- DEBIAN_FRONTEND=noninteractive apt-get -y -q upgrade >>"${log}"
echo "Check $log file for details."

See why we used DEBIAN_FRONTEND apt-get variable to avoid any prompts duding updates. It would be best if you set up ssh keys for automation purposes or running scripts from Linux/Unix cron jobs.

Finding ping time for multiple IP addresses

Here is my sample code:

ips="$(host -t a | awk '{ print $4}')"
for i in $ips; do ping -q -c 4 "$i"; done

This will produce average stats as follows, indicating that multiple load balancer ICMP ECHO_REQUEST average time was 19-20 milliseconds

PING ( 56(84) bytes of data.

--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 20.612/21.255/22.054/0.624 ms
PING ( 56(84) bytes of data.

--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 19.199/20.279/21.433/0.862 ms
PING ( 56(84) bytes of data.

--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 20.232/20.710/21.500/0.479 ms

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. For loops can save time and help you with automation for tiny tasks. However, for complicated IT automation tasks, you should use tools like Ansible, Salt, Chef, pssh and others. 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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 260 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
260 comments… add one
  • Jadu Saikia Nov 2, 2008 @ 15:37

    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 :


  • Sean Nov 4, 2008 @ 2:20

    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.

  • Andreas Nov 13, 2008 @ 4:53

    Nice explanation tutorial.

  • Manish Nov 25, 2008 @ 6:33

    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…


    • balamurugan Aug 11, 2010 @ 5:24

      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…

    • Manish Kumar Mishra Feb 3, 2011 @ 5:16

      It works properly just check it again..

    • Dr. Stefan Gruenwald Apr 1, 2011 @ 3:53

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

    • Niranjan May 10, 2011 @ 1:35

      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

      • krist0ph3r May 30, 2011 @ 14:37

        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

  • lascost Dec 6, 2008 @ 18:15

    i tried the last example but i seen dint work


    i would like know where is the error

    • Johnny Rosenberg Aug 6, 2015 @ 15:06

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

  • 🐧 nixCraft Dec 13, 2008 @ 16:56




  • lo2y Jan 28, 2009 @ 10:10

    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

  • swatkat Feb 12, 2009 @ 14:06

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

  • archana Mar 16, 2009 @ 23:53

    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

  • Navneet Apr 20, 2009 @ 10:57

    Good examples!! easily understood

  • firmit Jun 7, 2009 @ 17:06


    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!

    • 🐧 nixCraft Jun 7, 2009 @ 17:56


      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
      • Andi Reinbrech Nov 18, 2010 @ 19:42

        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

        • Dana Good Feb 13, 2012 @ 3:05

          This *is* helpful – good knowledge to have. I’ll go give it a try.


    • Alvin Aug 17, 2010 @ 12:09

      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


      • Philippe Petrinko Aug 17, 2010 @ 14:04


        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?

        • Philippe Petrinko Aug 17, 2010 @ 14:05

          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
  • firmit Jun 7, 2009 @ 18:03

    Excellent! Thanks Vivek.

  • Peko Jul 16, 2009 @ 18:11

    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

  • 🐧 nixCraft Jul 16, 2009 @ 18:31

    @ Peko,

    Thanks for pointing out ranges vs step value. I’ve updated the FAQ.

  • Peko Jul 16, 2009 @ 20:04


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

    — Peko

  • Michal Kaut Jul 22, 2009 @ 6:12


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


  • Peko Jul 22, 2009 @ 7:27

    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.


  • Peko Jul 22, 2009 @ 14:29

    To Michal Kaus [2]

    Ooops – ;-)
    Instead of LC_CTYPE,
    LC_NUMERIC should be more appropriate
    (Although LC_CTYPE is actually yielding to the same result – I tested both)

    By the way, Vivek has already documented the matter :

    — Peko

  • VIKAS Jul 22, 2009 @ 15:58

    Excellent stuff… keep up the good work.

  • Brad Landis Oct 26, 2009 @ 15:26

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

  • 🐧 nixCraft Oct 26, 2009 @ 16:47


    Try this without grep or cut using bash parameter expansion :

    echo $log
    echo $date


  • Philippe Petrinko Oct 26, 2009 @ 16:52

    H i vivek,

    Just wondering why you don’t amend the typo I pointed out:

  • Brad Landis Oct 26, 2009 @ 16:53

    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?

  • 🐧 nixCraft Oct 26, 2009 @ 18:37

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

  • TheBonsai Oct 30, 2009 @ 6:13


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

  • Philippe Petrinko Oct 30, 2009 @ 8:35

    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 !

  • tdurden Nov 10, 2009 @ 21:32

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

    • Dr. Stefan Gruenwald Apr 1, 2011 @ 4:00

      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

  • TheBonsai Nov 11, 2009 @ 7:07

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

  • Philippe Petrinko Nov 11, 2009 @ 11:25

    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

  • TheBonsai Nov 11, 2009 @ 11:49

    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.

  • Sean Nov 11, 2009 @ 14:42

    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)

    • A.R.Memon Dec 4, 2012 @ 9:58

      echo “Bash version ${BASH_VERSION}…”
      for i in {0..10..2}
      echo “Welcome to my new script $i times”

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

  • Philippe Petrinko Nov 11, 2009 @ 15:03

    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

  • Philippe Petrinko Nov 11, 2009 @ 15:14

    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:

  • Sean Nov 11, 2009 @ 20:10

    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 “\/$”)

  • Philippe Petrinko Nov 11, 2009 @ 21:16

    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

  • Philippe Petrinko Nov 11, 2009 @ 21:18

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

  • Philippe Petrinko Nov 11, 2009 @ 21:25

    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

  • Philippe Petrinko Nov 11, 2009 @ 21:27

    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

  • Philippe Petrinko Nov 11, 2009 @ 21:28
  • Narender Nov 12, 2009 @ 6:47

    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

    • 🐧 nixCraft Nov 12, 2009 @ 13:47


      Your post is offtopic. I suggest you use our shell scripting forum for question.

  • Sean Nov 12, 2009 @ 15:26

    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.

  • Philippe Petrinko Nov 12, 2009 @ 15:44

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

  • The_Catalanish Dec 4, 2009 @ 11:22

    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)

  • Rilif Dec 10, 2009 @ 21:27

    #! /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?

  • Philippe Petrinko Dec 11, 2009 @ 12:17

    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.

  • dee Dec 13, 2009 @ 10:27

    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.

  • Philippe Petrinko Dec 13, 2009 @ 10:54

    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.

  • 🐧 nixCraft Dec 13, 2009 @ 10:58

    There was no html tag or anything else in comment.

    @dee, if you need to attach code use <pre> tags.

  • Philippe Petrinko Dec 13, 2009 @ 11:44

    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 ?

  • 🐧 nixCraft Dec 13, 2009 @ 13:26


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

    echo "Hello world!"


  • dee Dec 13, 2009 @ 13: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

  • Philippe Petrinko Dec 13, 2009 @ 14:51

    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” ;-) ???

  • Dominic Jan 14, 2010 @ 10:04

    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.

  • Dominic Jan 14, 2010 @ 10:09

    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

  • Dmitry Mar 8, 2010 @ 17:47

    What if I would like to put everything into one line?

  • Dominic Mar 9, 2010 @ 5:29

    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.

  • Dmitry Mar 9, 2010 @ 13:56

    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?

  • Philippe Petrinko Mar 9, 2010 @ 14:05

    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”

  • Philippe Petrinko Mar 9, 2010 @ 14:34


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

  • Dmitry Mar 9, 2010 @ 14:49

    Philippe, thank you, all works perfect know.
    Here is the partial excuse that I was reading this thread instead of bash manual

  • Dominic Mar 9, 2010 @ 15:23

    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.

  • Dmitry Mar 9, 2010 @ 15:27

    Ok. Thank you again!

  • 🐧 nixCraft Mar 9, 2010 @ 17:30


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

  • TheBonsai Mar 9, 2010 @ 17:55

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

  • TheBonsai Mar 9, 2010 @ 18:06

    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.

  • Dominic Mar 9, 2010 @ 18:54

    @ 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

  • Philippe Petrinko Mar 9, 2010 @ 23:10

    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.

  • Sean Mar 9, 2010 @ 23:15

    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.

    • Andi Reinbrech Nov 18, 2010 @ 20:35

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


      • Philippe Petrinko Nov 18, 2010 @ 21:06


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

        Interesting comments anyway!

  • Philippe Petrinko Mar 10, 2010 @ 9:13

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

  • 2012 Doom Day Mar 10, 2010 @ 19:01

    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.

  • Philippe Petrinko Mar 10, 2010 @ 21:31

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

  • TheBonsai Mar 11, 2010 @ 11:30

    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.

  • Philippe Petrinko Mar 13, 2010 @ 21:50

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

  • TheBonsai Mar 14, 2010 @ 0:34


    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.

  • 2012 Doom Day Mar 14, 2010 @ 5:39


    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

  • TheBonsai Mar 19, 2010 @ 5:21

    @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, …

  • Philippe Petrinko Mar 19, 2010 @ 8:35

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

  • Chris Cheltenham Apr 2, 2010 @ 19:33

    Do you know why this doesn’t output anything?

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

  • Philippe Petrinko Apr 2, 2010 @ 20:47

    @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

  • Moihan May 9, 2010 @ 11:36

    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?

  • Anthony Thyssen Jun 4, 2010 @ 6:53

    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.

  • Philippe Petrinko Jun 4, 2010 @ 7:34

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

    • TheBonsai Jun 4, 2010 @ 9:57

      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.

      • Philippe Petrinko Jun 4, 2010 @ 10:15
      • Anthony Thyssen Jun 6, 2010 @ 7:18

        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.

        • TheBonsai Jun 6, 2010 @ 12:35

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

  • TheBonsai Jun 4, 2010 @ 17:57

    I’ve read the standard ;-)

  • Phil Goetz Jun 17, 2010 @ 17:38

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

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

    • 🐧 nixCraft Jun 17, 2010 @ 17:57

      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
  • Anthony Thyssen Jun 18, 2010 @ 0:49

    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!

    • Philippe Petrinko Jun 18, 2010 @ 7:01

      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'

      • Philippe Petrinko Jun 18, 2010 @ 7:02

        Hell ! missed again – I hate that $%@ Wordpress text entry

  • Paddy Jul 20, 2010 @ 20:54

    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

    • 🐧 nixCraft Jul 20, 2010 @ 21:12

      Try rename command:

      rename 's/\.01$//' *.01

      If you must use for:

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


      • Dr. Stefan Gruenwald Apr 1, 2011 @ 4:53

        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

        • maxy-millianne Jan 24, 2012 @ 6:18

          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.

          • Anthony Thyssen Jan 27, 2012 @ 7:48

            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

  • Sapia Jul 29, 2010 @ 6:20

    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.

    • Philippe Petrinko Jul 29, 2010 @ 11:09

      @Sapia: As Vivek may say,
      “Your post is off-topic. I suggest you use our shell scripting forum for question.”

  • borleand Sep 1, 2010 @ 20:23

    bash = GPL
    GPL = GNU
    GNU = Gnu is Not Unix

    UNIX logo in your web page = ???

    • 🐧 nixCraft Sep 2, 2010 @ 11:45

      Does it really matters?

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


      • Philippe Petrinko Sep 2, 2010 @ 12:10


        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.

      • Anthony Thyssen Sep 3, 2010 @ 2:02

        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.

  • naveen Sep 16, 2010 @ 4:53

    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

    • Philippe Petrinko Sep 16, 2010 @ 7:15

      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?

      • naveen Sep 16, 2010 @ 15:03

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

        • Philippe Petrinko Sep 16, 2010 @ 15:20

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

  • Anthony Thyssen Nov 19, 2010 @ 3:22

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

    • 🐧 nixCraft Nov 19, 2010 @ 7:58

      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.


  • Anthony Thyssen Nov 22, 2010 @ 1:16

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

  • Anthony Thyssen Nov 22, 2010 @ 1:17

    Nifty solution though, especially without access to a multi-character field separator.

    • Philippe Petrinko Nov 22, 2010 @ 7:58

      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}
      • Philippe Petrinko Nov 22, 2010 @ 8:23

        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

        • Philippe Petrinko Nov 22, 2010 @ 9:01

          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


          • Philippe Petrinko Nov 22, 2010 @ 11:28

            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)

  • Anthony Thyssen Nov 22, 2010 @ 23:31

    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.

    • Philippe Petrinko Nov 24, 2010 @ 18:29

      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

      • Philippe Petrinko Nov 24, 2010 @ 18:35


        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.


  • James Wilkinson Dec 14, 2010 @ 10:03

    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’

  • Sammeta Dec 15, 2010 @ 16:48

    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 :

    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,
  • Philippe Petrinko Dec 16, 2010 @ 18:10


    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.


  • mohan Jan 5, 2011 @ 0:29

    what does FILES=”@” do? Please explain i am new to unix.

  • John Jan 5, 2011 @ 1:24

    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 —

  • TheBonsai Jan 5, 2011 @ 7:09

    Wrong conclusion. This is not a reason for the seq idiom, it’s a reason to use arithmetically driven (C-like) for loops.

    for ((x = START; x <= END; x += INCR)); do
      echo $x

    • Philippe Petrinko Feb 3, 2011 @ 9:09


      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?


      • 🐧 nixCraft Feb 3, 2011 @ 12:15

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

        • Philippe Petrinko Feb 3, 2011 @ 13:56

          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?

          • TheBonsai Feb 3, 2011 @ 17:01

            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.

          • 🐧 nixCraft Feb 3, 2011 @ 17:29

            I think it was mentioned in’s advanced bash guide.

  • toto Feb 16, 2011 @ 5:01

    good tutorial.
    thank you

  • Víðir Valberg Guðmundsson Mar 8, 2011 @ 3:22

    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"? ;)


    • TheBonsai Mar 8, 2011 @ 6:26
      for ((i=1; i<=20; i++)); do printf "%02d\n" "$i"; done
    • 🐧 nixCraft Mar 8, 2011 @ 6:37

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

      for i in {01..20}; do echo "$i"; done
      • TheBonsai Mar 8, 2011 @ 6:48

        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"
  • ankit gulati Mar 12, 2011 @ 14:44


    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.

  • Rupender Mar 23, 2011 @ 15:48

    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

    • Philippe Petrinko Mar 23, 2011 @ 19:27


      following this topic,
      you may try to write something down first,
      and then we may help you … if you help yourself first.

      Read and use this topic,
      and the following material:

      However, such request would be best posted into the forum

      Anyway, a simple solution would be to use [ls] command with appropriate arguments.

  • Philip Ratzsch Apr 20, 2011 @ 5:53

    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!

    • Philippe Petrinko Apr 20, 2011 @ 8:23

      Nice thing to think of, using brace nesting, thanks for sharing.

  • David May 4, 2011 @ 10:10

    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.

    • David May 4, 2011 @ 10:11

      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”

  • Anthony Thyssen May 5, 2011 @ 2:57

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

    • Philippe Petrinko May 5, 2011 @ 13:10


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

  • Anthony Thyssen May 5, 2011 @ 3:01

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

  • Sanya May 6, 2011 @ 6:02


    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

    • Philippe Petrinko May 6, 2011 @ 10:13

      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

      • Philippe Petrinko May 6, 2011 @ 10:48


        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

        • Sanya May 6, 2011 @ 11:42

          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

          • Philippe Petrinko May 6, 2011 @ 12:07


            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


  • Anthony Thyssen May 8, 2011 @ 23:30

    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.

  • Tom P May 19, 2011 @ 12:16

    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

  • atef fawzy May 23, 2011 @ 10:27

    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

    • Philippe Petrinko May 23, 2011 @ 11:34

      No problem.
      Would you first send me 1000USD payment?
      Thanks in advance. ;-)

      • atef fawzy May 23, 2011 @ 12:37

        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


    • Philippe Petrinko May 23, 2011 @ 12:25

      Lucky You! Atef fawzy, Vivek has already written necessary training material for you: Read

      • atef fawzy May 23, 2011 @ 12:41

        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


  • atef fawzy May 23, 2011 @ 12:33

    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”


    • Philippe Petrinko May 23, 2011 @ 13:39

      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

  • atef fawzy May 24, 2011 @ 10:20

    please send me the full URL to post the error i got

  • Philippe Petrinko May 24, 2011 @ 11:14

    You can go where I already offered you =>

    You can go there and figure out what topic is appropriate to your needs.

  • loop how May 26, 2011 @ 0:49

    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;
  • Chu-Siang Jul 21, 2011 @ 21:07

    thanks for you example, It`s help me very much.

  • Faizanul Islam Jul 26, 2011 @ 6:04


    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.

  • brian Aug 5, 2011 @ 7:58


    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

  • Sam McAllister Aug 10, 2011 @ 11:10

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

  • Aug 11, 2011 @ 13:30

    Spot on. Included all examples that form a good base. Thanks for sharing.

  • Nagesh Sep 14, 2011 @ 10:21

    This post is 3 yrs old but still RockS.\m/

  • ptrac3 Oct 24, 2011 @ 17:43

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

    • Philippe Petrinko Oct 25, 2011 @ 9:01

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

    • 🐧 nixCraft Oct 25, 2011 @ 11:19

      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
  • Sonia McMath Nov 18, 2011 @ 6:52

    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

  • Hannah Dec 5, 2011 @ 23:21

    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?

  • Akshit Dec 7, 2011 @ 12:36

    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.

  • andreea Jan 5, 2012 @ 16:21

    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.

    • Philippe Petrinko Jan 5, 2012 @ 17:33

      Dear andreea

      1) I quote Vivek: “You are free to use our shell scripting forum for questions.”

      2) You can learn for free shell scripting with the infamous Vivek’s collaborative Linux Shell Scripting Tutorial


      — Philippe

      • Philippe Petrinko Jan 5, 2012 @ 18:23

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

        — Philippe

        • andreea Jan 6, 2012 @ 18:33

          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.

        • andreea Jan 8, 2012 @ 20:54

          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

  • Ram Jan 10, 2012 @ 18:57

    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.

    • the dsc Oct 23, 2012 @ 2:54

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

  • Philippe Petrinko Jan 27, 2012 @ 10:07

    Hi Ram,

    Since you are a real beginner, you would really take great benefit self-training on this course free of charge by vivek & contributors:


  • shahzad Mar 10, 2012 @ 9:04

    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,

  • regan May 5, 2012 @ 14:26

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

  • Excellent_Test Aug 23, 2012 @ 11:57

    This is what I was looking for. TLDP advanced guide is also good one.

  • Philippe Petrinko Nov 12, 2012 @ 19:38

    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”


  • Youshi Patel Feb 20, 2013 @ 15:14

    nice write up.

  • m.vinothini Mar 7, 2013 @ 6:17

    very useful

  • OSuKaRu Jun 6, 2013 @ 20:54

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

    Syntax error: Bad for loop variable


  • K J Ramana Rao Jun 20, 2013 @ 14:26

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


    • Anthony Thyssen Jun 24, 2013 @ 0:43

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

  • foolar Jun 21, 2013 @ 13:20

    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

  • abr Jun 27, 2013 @ 0:51

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

  • dennis bernaerts Jul 4, 2013 @ 13:19

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

  • Tring Tring Sep 24, 2013 @ 9:27

    Thank you so Much!

  • sana Oct 2, 2013 @ 10:40

    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

    • Philippe Petrinko Oct 2, 2013 @ 12:32

      no w cnt
      sry w dnt 1drstnd w U sd

      • sana Oct 3, 2013 @ 6:26

        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?

  • sana Oct 3, 2013 @ 6:31

    # 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…….. ../

  • Philippe Petrinko Oct 3, 2013 @ 7:48

    Well, it seems that you do not understand the basics of shell scripting, even a simple [mkdir] command.

    Anyway, you are very lucky, because this web site and Internet offer you _free_ training material.

    The good news is : all you have to do is learn and work, using for instance these links.
    and of course this page

  • matthias Oct 10, 2013 @ 5:19

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

    • Philippe Petrinko Oct 10, 2013 @ 9:18

      @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

  • ravindra Dec 3, 2013 @ 15:00

    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

    • Vamsi Dec 1, 2014 @ 11:18


      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.


  • RH User Jan 3, 2014 @ 21:12

    Thanks. Solved my coding issue.

  • dee Jan 17, 2014 @ 20:27

    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.

  • Dai Jun 18, 2014 @ 14:16

    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?

    • Philippe Petrinko Jun 19, 2014 @ 7:00

      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

      • Dai Jun 19, 2014 @ 7:33

        Marvelous. Cheers!

  • dronkit Apr 1, 2015 @ 20:55

    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.

  • Johnny Rosenberg Aug 6, 2015 @ 15:07

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

  • wt Sep 24, 2015 @ 13:06

    1 line loop, for example curling:
    for i in {1..500}; do curl “https://localhost:8040”; done;

  • onews Nov 3, 2015 @ 14:05

    How do we do something like:
    for file in /etc/* /bin/*

  • Carlos Luna Nov 14, 2015 @ 3:32

    Useful information! thank you!

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

  • Sasha Dec 22, 2015 @ 14:41


    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.


  • nezabudka Jan 3, 2016 @ 16:39

    for i in $(seq 1 2 $max)
    for i in $(eval echo {1..$max..5})

  • Emma Edwards Mar 18, 2016 @ 14:22

    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

  • Emma Edwards Mar 18, 2016 @ 14:23

    sorry i ment i cant seem to find….

  • Golla praveen Jun 14, 2016 @ 13:59

    for ((i=1;i<=100;i++));
    echo $i
    while excute the above program the below errors comes please check revert

  • mito Jul 1, 2016 @ 8:42

    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?

  • mito Jul 1, 2016 @ 8:45

    output is username und the file

  • Chad Nov 23, 2016 @ 15:41

    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.

  • Jignesh Dec 19, 2016 @ 7:36

    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


  • urvashi Feb 3, 2017 @ 12:58

    Very useful.Simple and easy.

  • Alex Mar 16, 2017 @ 20:32

    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
  • Danny May 23, 2017 @ 21:40

    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.

  • no Aug 31, 2017 @ 17:29

    Arrays start at 0. Kill yourself

  • Sundaram Sep 24, 2020 @ 14:32

    This helped. Thank you!

  • Jon Seidel Oct 1, 2020 @ 13:25

    The array syntax in the For loop with array example ( is incorrect — there are no commas required in the list; arrays should be coded as follows:
    MY_ARRAY=('one' 'two' 'three') # space-separated
    The arrays in the example are tacked on to the end of the previous string.

    • 🐧 Vivek Gite Oct 1, 2020 @ 16:23

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

  • David W Thomson Nov 26, 2020 @ 3:55

    How can we loop through a directory structure such as /2010/11/25/0.sac; where 0.sac is a range of files from 0 to 23? I have three years of earthquake data to run an FFT analysis on.

    • 🐧 Vivek Gite Nov 26, 2020 @ 8:40

      Try range feature as follows:

      for f in /2010/11/25/{0..23}.sac
              # $f is your filename now
      	echo "Working on $f file now ..."


  • thomas Jan 26, 2021 @ 8:16

    Seems helpful but my case I didn’t find. Regards

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum