为什么bash忽略结束双引号(")

时间:2017-08-24 04:59:21

标签: bash line-endings

我有这个文件:

while read p; do
        echo  "\"$p\""
done < file.txt

这个bash脚本:

"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/I_Cuestionario_general_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IV_Ingresos_y_recursos_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VI_ambito_escolar_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VII_ambito_laboral_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VIII_ambito_comunitario_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IX_Atencion_Obstetrica_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/X_ambito_familiar_estimaciones_endireh2016.xlsx

我希望相同的文件,但每行左右有双引号,但这就是bash输出的内容:

"

任何人都知道为什么bash会这样表现?以及如何输出 Date ID 0 2016-3-28 00:56:43 1 1 2017-3-28 00:56:43 1 2 2005-3-28 00:56:43 1 3 2010-3-28 00:56:43 1 4 2002-3-28 00:56:43 1 5 2017-3-29 00:56:43 1 6 2004-3-28 00:56:43 5 7 2002-3-21 00:56:43 5 8 2015-3-28 00:56:43 5 9 2017-4-28 00:56:43 5 10 2009-3-28 00:56:43 5 11 2007-3-28 00:56:43 5 双引号? (开始和结束)

2 个答案:

答案 0 :(得分:3)

我确定输入文件中的行结尾是CR / LF而不是LF。这将输出:

  • ";
  • 网址;
  • CR将光标返回到行的开头;
  • ";最后,
  • 搬到新线路。

将输出捕获到文件并通过转储实用程序(如od -xcb)传递,该实用程序应显示正在输出的原始字节。

作为测试,创建一个由两行123<CR>456组成的文件,我看到了:

pax> while read p; do echo "\"$p\""; done <testfile
"123
"456"

似乎表明问题如上所述。

答案 1 :(得分:0)

如果您在转发前导和尾随双引号时遇到问题,则可以在echo语句周围使用单引号。单引号内的任何双引号在定义字符串文字方面没有意义,反之亦然:

while read p; do
        echo '"$p"'
done < file.txt