String concatenation doesn’t work in bash

CRLF vs LF (again)

This time I was pulling my hair for a weird behavior in bash when concatenating strings in a bash script.

#!/bin/bash

# Read the version numbers from the file
variable1=$(grep VERSION_MAJOR file.txt | cut -d' ' -f 2)
variable2=$(grep VERSION_MINOR file.txt | cut -d' ' -f 2)
result="${variable1}.${variable2}"

echo "${result}"

The intention of this script is to echo two concatenated strings coming from storing a command in those variables.  But echo was showing only the value or ‘variable2’.

After a couple of hours blaming bash version, OS version, and searching online I found this question/answer in stackoverflow blaming ‘carriage return’.

And then again, I remembered that this file I was trying to parse was coming from a windows environment. 

I checked with 

cat -A file.txt

and indeed it was containing the windows carriage return. 

So I removed the CRLF characters using the tool ‘dos2unix’ and after running the bash script it shows now the expected result.