bash for loop includes wild characters (*.c) if no

2019-02-24 23:44发布

I am using bash on UNIX (sparc 10)

for file in $SCPATH/$LIBNAME/*.{gob,c,cpp,h};
do
    ln -s $file;
done;

The problem is: if there are no files with extension 'c', it will put ".c" in $file and ln -s will create a link to '.c'. Is this a know issue? How can I get around it (besides the obvious 'if not *.c' hack).

1条回答
Explosion°爆炸
2楼-- · 2019-02-24 23:52

you need to set nullglob before your loop

shopt -s nullglob

nullglob: If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves.

When you're done and want to reset it to the original behavior, use:

shopt -u nullglob

As pointed out by Dennis Williamson in one of the comments.

查看更多
登录 后发表回答