简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM