简体   繁体   English

bash中字符串的串联

[英]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. 我在尝试从WordPress自述文件解析版本号时遇到以下bash脚本问题。

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; readme.txt和/或您的脚本具有DOS行尾; the value of VERSION has a trailing carriage return which affects the output. VERSION的值具有尾随回车符,这会影响输出。

If you pipe the output to cat -A you will likely find out that $VERSION contains a carriage return. 如果将输出传递给cat -A ,则可能会发现$VERSION包含回车符。

You can get rid of the CRs with tr : 您可以使用tr摆脱CR:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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