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

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -