HowTo: Unix For Loop 1 to 100 Numbers

Posted on in Categories , , last updated July 16, 2011

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.

Ksh For Loop 1 to 100 Numbers

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

Bash For Loop 1 to 100 Numbers

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

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:

while [[ $c -le 100 ]]
  # your-unix-command-here
   echo "$c"
   let c=c+1

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

5 comment

  1. # display 1 to 10 numbers using while loop
    # vi
    # !.bin/sh
    while [[ $a -le 10 ]]
    echo “$a”
    a= ` expr a + 1 ` or let a=a+1

    # now it display 1 to 1o numbers

Leave a Comment