2012-08-29 39 views
4

我使用Bash脚本从文本文件中逐行读取,该文本文件中包含特殊字符(正则表达式)。当我使用echo "${SOME_VAR}"时,它不会像那样显示文本Bash在脚本中拦截通配符

我熟悉Prevent * to be expanded in the bash script

如何显示和使用文本?

UPDATE 文本(TSV)文件包含类似的元组(最后一个条目是psql的查询)

bathroom bathroom select name from photos where name ~* '\mbathroom((s)?|(''s)?)\M'; 

我读了CSV如下:

tail -n+2 text.file | while IFS=$'\t' read x y z 
do 
    echo "${z}" 
done 

这给出输出

select name from photos where name ~* 'mbathroom((s)?|(''s)?)M'); 

注意 '\' 丢失

+2

向我们展示您的输入和一些代码请 –

+0

@Fredrik请参阅我的更新。 – MrRoth

+0

“tail”过程的替代方法(避免可能导致子外壳问题的管道)将会是{{read;读;而IFS = $'\ t'read z y z;做...;完成; } chepner

回答

3

尝试使用-r标志与read

tail -n+2 text.file | while IFS=$'\t' read -r x y z 
do 
    echo "${z}" 
done 

read手册页:

-r不以任何治疗反斜杠字符特殊的方式。考虑每个反斜杠是输入行的一部分。

+0

我不能相信我错过了。谢谢! – MrRoth