[RndTbl] bash expansion question
Trevor Cordes
trevor at tecnopolis.ca
Wed Jul 7 02:22:23 CDT 2010
On 2010-07-06 Adam Thompson wrote:
> I can't find what I'm looking for in the bash(1) manpage, hoping
> there's an easy answer...
>
> I want to use a construct like
> for i in ~/path/*; do something $i && rm $i; done
>
> which works great as long as there are files in ~/path/. However,
> when the directory is empty, I get:
> rm: cannot remove `/home/athompso/path/*': No such file or
> directory
What you want is:
shopt -s failglob
for i in /tmp/A/* /dev/null; do echo $i; done
bash: no match: /tmp/A/*
# gives an error but doesn't exec the do clause
or
shopt -s nullglob
for i in /tmp/A/* ; do echo $i; done
# outputs nothing, executes no do clause
Using these options lets you still do sane arg quoting in case there
are spaces in filenames, etc.
More information about the Roundtable
mailing list