Search code examples
bashshelltrimtr

Shell: Robust way to remove trailing decimal point and zero in case there is a zero


In a bash script I have a (two digit) number with decimal point and one digit following.

In case there's a zero after the decimal separator, I want to trim the separator and the zero.

For

foo=26.5

..there should be no trimming. But in this case:

foo=26.0 

My desired output would be:

foo=26

Is tr the right choice for it?

I tried it with the --delete .0 option but it simply deletes all of both character's appearences.

Thank you!


Solution

  • Not only for bash:

    foo=$(echo "$foo" | sed 's/\.0$//')