Search code examples
stringbashconcatenationstring-concatenation

Bash - disappearing values while concatenating strings


I am trying to insert into the file values which I get from another file (xml) and I want them to be in one row. I checked variables - separately they are but once I try to concatenate them somehow, something goes wrong and I get only parts of the prior strings.

My code looks as it follows:

echo $usd
echo $gbp
all=$usd$space$gbp
echo $all

The output looks like this:

3,6525
5,5407
 5,5407

Does anybody know what am I doing wrong? Or what can I do to restore my data?


Solution

  • You have a CR in your variable. I can reproduce your case with the vars

    usd="3,6525^M"
    gbp="5,5407^M"
    space=" "
    

    (I entered ^M in vi with CTRL-V CTRL-M, it is the \r character) When you do not want to clean the xml, usd and gbp, you can use

    all=${usd%$'\r'}${space}${gbp%$'\r'}