How To Unix For Loop 1 to 100 Numbers

I want to run a Unix command 100 times using a for loop from 1 to 100. Can you tell me how to take a block of numbers in a loop under KSH or BASH shell? How can I iterate through a range of integers numbers in ksh or bash under Unix systems?

You can use the following syntax to run a for loop and span integers.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash/sh/ksh on Linux or Unix
Est. reading time 1 minute

POSIX syntax

The syntax is as follows using while loop to count 1 to 5 numbers:

#!/bin/sh
i=0
while [ $i -ne 5 ]
do
        i=$(($i+1))
        echo "$i"
done

For bash shell replace the line:

#!/bin/sh

With:

#!/bin/bash --posix

Ksh For Loop 1 to 100 Numbers

#!/bin/ksh
# Tested with ksh version JM 93t+ 2010-03-05
for i in {1..100}
do
 # your-unix-command-here
 echo $i
done

We can use while loop too:

#!/bin/ksh
i=1
while [[ $i -lt 1000 ]] ; do
   # your unix command here #
   echo "$i"
  (( i += 1 ))
done

C-like for loop syntax for ksh93/bash/zsh

The syntax is as follows:

#!/bin/ksh93
for ((i=1; i<=20; i++))
do
    # Unix command here #
    echo "$i"
done

Bash For Loop 1 to 100 Numbers

#!/bin/bash
# Tested using bash version 4.1.5
for ((i=1;i<=100;i++)); 
do 
   # your-unix-command-here
   echo $i
done

Dealing With Older KSH88 or Bash shell

The above KSH and Bash syntax will not work on older ksh version running on HP-UX or AIX. Use the following portable syntax:

#!/usr/bin/ksh
c=1
while [[ $c -le 100 ]]
do 
  # your-unix-command-here
   echo "$c"
   let c=c+1
done

Here is script that is tested with older version of KSH running on Sun Solris and HP-UX/AIX:

#!/bin/ksh
i=1
echo "Counting from 1 to 10: "
while (( $i <= 10 ))
do
   echo "$i"
  (( i=$i+1 ))
done

Outputs:

Counting from 1 to 10: 
1
2
3
4
5
6
7
8
9
10

Say hello to seq for iterating through a range of ints in ksh?

The following command displkay numbers from FIRST to LAST, in steps of INCREMENT:
seq FIRST LAST
seq FIRST INCREMENT LAST

Iterating through a range between 1 to 10 in ksh is as follows:
seq 1 10
To increment value by 2:
seq 1 2 10
For example:

#!/bin/ksh
for n in `seq 1 100`
do 
  echo "Hello, $USER. [$n]"
done


The seq command works regardless of shell version and we can use variables too:

START=5
END=10
for i in $(seq $START $END)
do
   echo "Doing something with $i ..."
done
Doing something with 5 ...
Doing something with 6 ...
Doing something with 7 ...
Doing something with 8 ...
Doing something with 9 ...
Doing something with 10 ...

Conclusion

We learned various methods to count numbers between 1 to 100 when using KSH, bash, sh and other shell running on Linux or Unix-like systems. See man pages:
$ man ksh
$ man bash
$ man sh


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

🐧 11 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
11 comments… add one
  • Ewerton Ciuffo Mar 28, 2014 @ 18:25

    This script worked for me in AIX

    #!/bin/bash
    for i in {1..10}
     do
        echo $i
     done
    
    • Chris Pudney Jan 27, 2015 @ 0:37

      The Ksh example works in bash (on Ubuntu 14.10)

  • kondareddy Nov 16, 2015 @ 4:52

    Display 1 to 10 numbers using while loop
    # vi while.sh
    Code:

    #!bin/sh
    a=1
    while [[ $a -le 10 ]]
    do
    echo "$a"
    a=`expr a + 1` 
    # or use: let a=a+1  #
    done

    Now it display 1 to 1o numbers

  • Chris May 16, 2016 @ 1:10

    seq 1 100

    • The Bear Oct 7, 2020 @ 13:09

      This answer is by far the best one. Just strange that it does not come on the first line of this page :-D

  • bubu Feb 10, 2017 @ 3:36

    # print 1 to 100
    for i in `seq 1 100`; do echo $i; done

  • Dave Apr 29, 2021 @ 15:28

    POSIX compliant:

    i=1
    while [ $i -ne 11 ]
    do
            #echo $i
            printf "%02d\n" $i
            i=$(( $i + 1 ))
    done
    

    The “[[” form is not POSIX.

  • Mark Jun 15, 2021 @ 1:56

    Hi Vivek Gite
    Thank you so much for your tutorials they are very helpful.
    I am trying to make a simple for loop counting up, but the target number is a variable.
    i.e.

    for i in {1..$someNumber}

    like your example

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

    except i need to run the loop to 10 or any other predetermined number but to a number I figure out in the bash script. A variable.
    M.

    • 🐧 Vivek Gite Jun 15, 2021 @ 6:32

      Use the seq command

      someNumber=5
      for i in $(seq 1 $someNumber)
      do
          echo "$i"
      done

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