1

I want to print the numbers in the following pattern using WHILE loop.

0
01
012
0123
.....
......
0123456789

My Try:

#!/bin/sh

a=0
b=0 

while [ $a -le 10 ]
do  
        while [ $b -le $a ]
        do
                echo -n "$b"
                b=`expr $b + 1`
        done
        echo
        a=`expr $a + 1`
done    

Getting output:

0
1
2
3
4
5
6
7
8
9
10
4
  • Have you realized that you only print the b value which is incremented in each iteration? In other words, have you thought about the algorithm? Commented Jun 23, 2016 at 8:24
  • You should use a variable to save the last output (012). Then you should append it the value of b and print it and increase the counter. Commented Jun 23, 2016 at 8:28
  • Is there a specific requirement to use a while loop? What shell are you using? expr is a non-standard, old-fashioned way of doing arithmetic in the shell. Commented Jun 23, 2016 at 8:56
  • @MAK, as you accepted the Ben's answer as a solution, can I assume the shell you are using is bash, not any other UNIX shell as you have tagged this question? Commented Jun 23, 2016 at 9:40

3 Answers 3

4

As you simply append the latest count to the line output, simply do so as text.

#!/bin/bash

a=0
out=''

while [ $a -lt 10 ]
do
    out=$out$a
    echo $out
    a=`expr $a + 1`
done

Also, le is less or equal, so you end up with 10. Use lt 10 or le 9.

Sign up to request clarification or add additional context in comments.

1 Comment

Why not use a=$(( a + 1 )) instead of expr? It should work in any POSIX-compliant that isn't too old.
1

If you use bashshell, you can take advantage of sequence expressions of the form {x..y} and use the special parameter $_ which usually expands to the last argument to the previous command.

#/bin/bash
i=
for i in {0..9}
do
     echo "$_$i"
done

Comments

0
a=0                                                                           
b=1                                                                                             
while [ $a -lt 10 ]                                                                         
do                                                                                                                                                               
    a=0                                                                                                                                                      
    while [ $a -lt $b ]                                                                                                                                      
    do                                                                                                                                                       
    echo -n $a                                                                                                                                               
    a=`expr $a + 1 `                                                                                                                                         
done                                                                                                                                                     

echo                                                                                                                                                             

b=`expr $b + 1`                                                                                                                                                  

done  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.