Generate numbers 000 to 999

I have tried to make this script to generate:

000
001
002
...
997
998
999

i=0
while [ $i -le 999 ]
do
if [ $i le 9 ]
then
echo "00"$i
else if [ $i -gt 9 ] && [ $i -le 99 ]
then
echo "0"$i
else
echo $i
fi
i=`expr $i + 1`
done

test:
~$ ./echonum
./echonum: line 14: syntax error near unexpected token `done'
./echonum: line 14: `done'

What is the problem with this?

Try:

for i in `seq 0 999`; do printf "%03d\n" $i; done

"else if" should be "elif"

perl -e 'printf "%03d\n", $_  for (0..999);'
$ ruby -e 'print ("000".."999").to_a.join(" ")'

No need to call external utilities such as seq. The following works for ksh93 and bash.

for i in {0..999} 
do
    printf "%03d\n" $i
done

or even simpler

printf "%03d\n" {0..999}
1 Like