10

I'd like to generate a sequence of equally spaced decimal numbers.

For example, I want to echo all numbers between 3.0 and 4.5, with step 0.1. I tried $ for i {3.0..4.5..0.1}; do echo $i; done, but this gives an error.

I also tried $ for i in $(seq 3.0 4.5 0.1); do echo $i; done but nothing happens.

2
  • 1
    Bash doesn't do floating point numbers. You're best off switching to a different scripting language. Commented Jun 10, 2015 at 22:51
  • Is there a command to replace seq that can generate that list so I can use it? Commented Jun 10, 2015 at 22:59

3 Answers 3

25

I also tried $ for i in $(seq 3.0 4.5 0.1); do echo $i; done but nothing happens.

The order is wrong:

$ for i in $(seq 3.0 0.1 4.5); do echo $i; done 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thanks this works great. Much easier to read than the other methods.
5

If you're looking for a loop from 3.5 to 4.5 in 0.1 steps this would work

for x in {35..45}; do y=`bc <<< "scale=1; $x/10"` echo $y done 

The same with 0.01 steps

for x in {350..450}; do y=`bc <<< "scale=2; $x/100"` echo $y done 

3 Comments

Thanks, this works. Is bc compulsory for the division?
Just googled it, now I'm using for x in {30..45}; do y=$(dc <<< "1k $x 10/p"); echo $y; done.
Yeah, either one works. bash only support integer arithmetic. Therefore, one always needs to use another program...
-2
 for i in {3.0,4.5,0.1}; do echo $i; done 

5 Comments

No, that only prints three lines 3.0, 4.5 and 0.1.
What do you want to print ??
A range of decimal numbers, e.g. 3.0 3.1 3.2 3.3 ... 4.5
@izxle - The original question was prone to misunderstandings. The syntax shown here is useful for what it was meant to.
What this syntax is "meant to do", however, has no reasonable relationship with "loop[ing] through a range of decimal numbers".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.