kilojoules
kilojoules

Reputation: 10083

shell iterate through custom list of numbers

I can iterate through evenly-spaced sequences of numbers using seq or a c-style for loop:

$ for (( i = 1; i < 6 ; ++i)); do echo $i ; done

1
2
3
4
5

$ for i in $(seq 1 5); do echo $i; done
1
2
3
4

I want to generate an irregular sequence, 1 2 4 4.25 4.5 5. What is a simple way to iterate through these numbers in a shell loop?

Upvotes: 2

Views: 5105

Answers (1)

Unwastable
Unwastable

Reputation: 679

If you are using aubhava's approach, you can also display decimal places in your output:

for i in 1 2 4 4.25 4.5 5; do
    printf '%0.02f\n' $i
done

output:

1.00
2.00
4.00
4.25
4.50
5.00

Upvotes: 7

Related Questions