user2985710
user2985710

Reputation: 153

Concatenation of strings in bash

I'm having issues with the following bash script trying to parse a version number from a WordPress readme file.

cat readme.txt | {
    while read -r a b c d; do
    if [ ${a} == "Stable" ]  && [ ${b} == "tag:" ]; then
        VERSION="$c"
    fi
done
out="Updated to version $VERSION thanks"
echo $out
}

The output I expect is

Updated to version 1.15 thanks

but the actual output is

 thanks to version 1.15

as though the 'thanks' is replacing the front of the string, not being appended to the end. Any clues?

Upvotes: 1

Views: 114

Answers (2)

Martin Vidner
Martin Vidner

Reputation: 2337

If you pipe the output to cat -A you will likely find out that $VERSION contains a carriage return.

You can get rid of the CRs with tr:

$ echo $'foo\rb'
boo
$ echo $'foo\rb' | tr -d '\r'
foob

Upvotes: 1

chepner
chepner

Reputation: 531155

readme.txt and/or your script has DOS line endings; the value of VERSION has a trailing carriage return which affects the output.

Upvotes: 4

Related Questions