Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When i run

p=0.1
k=2
while [ $p \< 1 ]
do
    echo $p
    p=$(echo "scale=2; $p*$k" | bc)
done

i have this result

0.1
.2
.4
.8

What can i do to have 0.1, 0.2, 0.4 and 0.8 ?

share|improve this question

closed as off topic by Rinzwind, hhlp, LnxSlck, Jorge Castro, Tachyons Nov 20 '12 at 18:47

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Before the echo check with an if if the 1st char of p is a point and if so add a 0 in front of it? –  Rinzwind Nov 20 '12 at 15:36
1  
this is a pure programming question and probably belongs on stackoverflow instead... –  ImaginaryRobots Nov 20 '12 at 16:17

2 Answers 2

Use printf instead of echo to format it:

printf %0.1f\\n $p

Output:

$ p=0.1; k=2
$ while [ $p \< 1 ]; do     printf %0.1f\\n $p ;     p=$(echo "scale=2; $p*$k" | bc); done
0.1
0.2
0.4
0.8
share|improve this answer

I would do use seq and printf to solve my problem. check this:

for i in $(seq .1 .2 1); do printf "%0.1f\n" "$i"; done

Maybe this will help you to continue.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.