问题描述
在一个像这样的循环,
for i in `cat *.input`; do
echo "$i"
done
如果输入文件之一,包含像项* A
,会,并给
文件名中的'A'的结局。
if one of the input file contains entries like *a
, it will, and givethe filenames ending in 'a'.
有$ P $的pventing这个文件名扩展一个简单的方法?
Is there a simple way of preventing this filename expansion?
由于使用多个文件,通配符(设置-o noglob
)不是
好的选择。我也应该能够猫的输出滤波器
来
特殊字符转义,而
Because of use of multiple files, globbing (set -o noglob
) is not agood option. I should also be able to filter the output of cat
toescape special characters, but
for i in `cat *.input | sed 's/*/\\*'`
...
还是引起了 *一个
扩大,而
for i in `cat *.input | sed 's/*/\\\\*'`
...
给我 \\ *一个
(包括反斜杠)。 [我想这是一个不同的
虽然问题]
gives me \*a
(including backslash). [ I guess this is a differentquestion though ]
推荐答案
这将猫
所有文件的内容的和迭代结果行:
This will cat
the contents of all the files and iterate over the lines of the result:
while read -r i
do
echo "$i"
done < <(cat *.input)
如果文件的包含的的通配符,他们将不会扩大。这些键是不使用为
并引用您的变量。
If the files contain globbing characters, they won't be expanded. They keys are to not use for
and to quote your variable.
在不支持进程替换伯恩衍生弹,这相当于:
In Bourne-derived shells that do not support process substitution, this is equivalent:
cat *.input | while read -r i
do
echo "$i"
done
的理由不这样做,在Bash是它创建一个子shell,当子shell(环)出口,内设置任何 CD
目录更改变量的值将丢失。
The reason not to do that in Bash is that it creates a subshell and when the subshell (loop) exits, the values of variables set within and any cd
directory changes will be lost.
这篇关于如何prevent文件名扩展为在bash循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!