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
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 7 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
This script worked for me in AIX
The Ksh example works in bash (on Ubuntu 14.10)
Display 1 to 10 numbers using while loop
# vi while.sh
Code:
Now it display 1 to 1o numbers
seq 1 100
This answer is by far the best one. Just strange that it does not come on the first line of this page :-D
Added seq command too.
# print 1 to 100
for i in `seq 1 100`; do echo $i; done