sh read command eats slashes in input?

℡╲_俬逩灬. 提交于 2019-12-18 05:28:07

问题


Perhaps easiest to explain with an example:

$ echo '\&|'
\&|
$ echo '\&|' | while read in; do echo "$in"; done
&|

It seems that the "read" command is interpreting the slashes 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?


回答1:


Accrding to: http://www.vias.org/linux-knowhow/bbg_sect_08_02_01.html :

-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.

It works on my machine.

$ echo '\&|' | while read -r in; do echo "$in"; done
\&|



回答2:


Use read -r, as per http://www.ss64.com/bash/read.html:

-r
If this option is given, backslash does not act as an escape character.



来源:https://stackoverflow.com/questions/924388/sh-read-command-eats-slashes-in-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!