bash - "for" loop wildcard evaluated to variable if no such files present -
$ f in /etc/shell*; echo $f; done /etc/shells $
good!
$ f in /etc/no_such*; echo $f; done /etc/no_such* $
bad!
how can reap off wildcard evaluation if no files present?
there specific shell option enable behaviour globs, called nullglob
. enable it, use shopt -s nullglob
.
when option enabled, pattern no matches evaluates nothing, rather itself.
this non-standard feature provided bash, if you're using shell or looking more compatible option can add condition loop body:
for f in /etc/no_such*; [ -e "$f" ] && echo "$f"; done
this echo if file exists.
Comments
Post a Comment