d-man
d-man

Reputation: 496

Bash: switch from integer to float

I am iterating over a range of numbers but would like to switch from an integer to a floating point number with one decimal place (e.g. from 5 to 5.0) as illustrated by the loop below.

for coul in 5 6 7; do

    mkdir cc_${coul}                                      # <-- here, ${coul} 
                                                          # should be an integer

    ... some code ... 

    sed -i '15s/.*/variable coul equal '${coul}'/' cc.in  # <-- here, ${coul}
                                                          # must be a decimal 
                                                          # (e.g. 5.0)
done

Upvotes: 0

Views: 3466

Answers (2)

rici
rici

Reputation: 241701

You can write:

"${coul}.0" 

The braces are unnecessary in a Posix shell or in bash because . is not a valid identifier character, but they might be necessary in other shells. They certainly don't hurt.

Upvotes: 2

chepner
chepner

Reputation: 531075

Use a different variable; as far as bash is concerned, these are just strings that could be treated as numbers in the right context.

coul_f=$coul.0
sed -i '15s/.*/variable coul equal '"$coul_f"'/' cc.in

Upvotes: 0

Related Questions