14

Consider the following echo command:

 echo -e "at\r"

which produces the output at on the command line, i.e. the \r special character has been interpreted. I want to do the exact same thing with some text in a file. Supposing the exact same sequence

at\r

is written to a file named at.txt, then I want to display it on the terminal. But

cat at.txt

gives the output

at\r

what is not what I want. I want the special sequence \r to be interpreted, not just printed on the terminal. Anyone any idea?

Thanks Alex

4

3 回答 3

14

为什么不:

while read -r line; do echo -e $line; done < at.txt
于 2012-08-29T10:23:31.183 回答
9

您可以简单地:

echo -e $(cat at.txt)
于 2015-12-10T14:31:30.163 回答
2

内置echo命令解释常见的反斜杠转义。但是在文件中,您必须以类似的方式解释或转换它。该sed程序可以做到这一点。

sed -e 's/\\r/\r/' < at.txt

但我也在这里学到了一些东西。外部echo命令的行为与内部命令不同。

/bin/echo "\r"

有不同的输出

echo "\r"

但基本上你需要一个过滤器将文字\r字符串转换为单字节 0x0D。

于 2012-08-29T10:23:19.370 回答