Search code examples
bashshellseq

Retain decimal portion of value in seq command?


I'm working on a script that needs to locate an Apple SDK for cross-compiling from the command line. The SDK names have a particular format and it includes a version number:

  • iPhoneOS7.0.sdk
  • iPhoneOS7.1.sdk
  • iPhoneOS7.2.sdk

When I run the following script using the seq command, the decimal portion is dropped when the decimal portion is 0. That is, 6.0 is displayed as 6. The other values are OK:

for i in $(seq 20.0 -0.1 4.3)
do
   echo "Value: $i"
done

For example:

$ ./count.sh 
Value: 20
Value: 19.9
Value: 19.8
Value: 19.7
Value: 19.6
Value: 19.5
Value: 19.4
Value: 19.3
Value: 19.2
Value: 19.1
Value: 19
...

I'm not sure if seq is not producing the trailing 0, or if echo is dropping the trailing 0. In either case, I need to retain it.

How do I retain the decimal portion of the value i?


Solution

  • seq(1) takes a printf(3) style formating strings with the argument -f, so you can do seq -f "%.1f" 20.0 -0.1 4.3.

    Which gives:

    $ seq -f "%.1f" 1 0.1 2
    1.0
    1.1
    1.2
    1.3
    1.4
    1.5
    1.6
    1.7
    1.8
    1.9