≡ Menu

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.

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
Share this tutorial on:

{ 4 comments… add one }
  • Ewerton Ciuffo March 28, 2014, 6:25 pm

    This script worked for me in AIX

    #!/bin/bash
    for i in {1..10}
     do
        echo $i
     done
    
  • kondareddy November 16, 2015, 4:52 am

    # 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 am

    seq 1 100

Leave a Comment


   Tagged with: , , , , , , , , , , , , , , ,