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

You can use the following syntax to run a for loop.

ADVERTISEMENTS

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

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 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
    # !.bin/sh
    a=1
    while [[ $a -le 10 ]]
    do
    echo “$a”
    a= ` expr a + 1 ` or let a=a+1
    done

    # now it display 1 to 1o numbers

  • Chris May 16, 2016 @ 1:10

    seq 1 100

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.