bash for loop through no file -
bash for loop through no file -
when loop through files starting foo
do
for f in foo* ; echo "result = $f" ; done
the problem when no file start foo
get:
result = foo*
meaning loop executed once, if no file start foo
.
how possible? how can loop through files (and not loop @ if there no file)?
you can stop behaviour setting nullglob:
shopt -s nullglob
from linked page:
nullglob
bash shell alternative modifies [[glob]] expansion such patterns match no files expand 0 arguments, rather themselves.
you can remove setting -u
(unset, whereas s
set):
shopt -u nullglob
test $ touch foo1 foo2 foo3 $ file in foo*; echo "$file"; done foo1 foo2 foo3 $ rm foo*
let's see:
$ file in foo*; echo "$file"; done foo*
setting nullglob
:
$ shopt -s nullglob $ file in foo*; echo "$file"; done $
and disable behaviour:
$ shopt -u nullglob $ file in foo*; echo "$file"; done foo*
bash for-loop wildcard
Comments
Post a Comment