如何在Centos7中正确回显变量

问题描述

我有一个要像这样回显的版本值

The file version 1.1 is available

为此,我正在使用$ version变量,该变量包含值1.1。但是无论出于什么原因,我都无法回应这一行。它总是给我结果-

 is available

echo命令仅打印$ version之后的内容。这些是我尝试过的echo命令

echo "The file version" ${version} "is available"
echo "The file version ${version} is available"
echo "The file version $version is available"
echo "The file version" "$version" "is available"
echo "The file version" "${version}" "is available"

我什至使用echo -nprintf命令尝试了相同的操作,但是它不起作用。我在这里想念什么?

解决方法

根据您看到的输出,我相当确定您的$version变量包含回车符('\r')字符。该值已被打印,但是在'\r'将光标发送到行首之后被覆盖。

您可以通过输入

进行验证
printf "%q\n" "$version"

echo "$version" | cat -A

您可能做过类似的事情:

version="$(some_command some_option)"

其中的输出使用Windows样式的行尾。

避免这种情况(也许不是最干净的解决方案)的许多方法之一是:

version="$(some_command some_option | tr -d '\r')"