I have a variable containing four numbers separated by a space, such as for instance:
a="12.3 423.4 11.0033 14.02"
But sometimes, I have a trailing whitespace:
a="12.3 423.4 11.0033 14.02 "
I want to replace the spaces with " & ", and for that, I do:
echo ${a// / & }
Which gives me:
12.3 & 423.4 & 11.0033 & 14.02
or if I have a trailing whitespace:
12.3 & 423.4 & 11.0033 & 14.02 &
My problem is that I don't know if I'll have a space at the end of my string and I don't want that extra "&" in any case. What would be the most elegant way to avoid this extra character? Is there a way to say "replace if a space and the next character a digit"?
Edit: I knew I could use sed, but since there is a mechanism of variable substitution in bash, I would like to know how could I use it to do what I want. I don't know how to write "not end of line" or "is a digit" in the bash substitution.