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.

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

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


🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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
7 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

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz