sh read 命令在输入中使用反斜杠? [英] sh read command eats backslashes in input?
问题描述
也许用一个例子最容易解释:
Perhaps easiest to explain with an example:
$ echo '\&|'
\&|
$ echo '\&|' | while read in; do echo "$in"; done
&|
似乎 read
命令将输入中的反斜杠解释为转义符并删除它们.我需要逐行处理文件而不更改其内容,而且我不知道如何在此处停止读取智能.有什么想法吗?
It seems that the read
command is interpreting the backslashes in the input as escapes and is removing them. I need to process a file line by line without changing its contents and I'm not sure how to stop read from being smart here. Any ideas?
推荐答案
根据:http://www.vias.org/linux-knowhow/bbg_sect_08_02_01.html:
-r
如果给出此选项,反斜杠不会充当转义字符.反斜杠被认为是一部分行的.特别是,一个不能使用反斜杠换行对作为行的延续.
-r
If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
它适用于我的机器.
$ echo '\&|' | while read -r in; do echo "$in"; done
\&|
这篇关于sh read 命令在输入中使用反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!