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

Why is bash ignoring the ending double quote (")

我有这个文件:

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 脚本:

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 会这样吗?以及如何输出两个 " 双引号? (开始和结束)

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

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

我几乎可以肯定您输入文件的行尾是 CR/LF 而不仅仅是 LF。这将输出:

  • ";
  • 网址;
  • a CR 将光标返回到行首;
  • ";最后,
  • 换行。

将输出捕获到一个文件,并通过像 od -xcb 这样的转储实用程序传递它,它应该会显示正在输出的原始字节。

作为测试,创建一个包含两行 123<CR>456 的文件,我看到:

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

这似乎表明问题与描述的一样。