Globing in bash

There is a pretty common and unnecessary pattern used by bash scripts: whenever you need to loop through a list of file names in a path, you might tempted to write something like this.

for fname in $(ls | grep foo); do echo $fname; done

You can save some typing by using bash-globbing:

for fname in *foo*; do echo $fname; done

Not only the script should be cleaner and faster, bash will take care of properly expanding the file names and you won’t have to worry about things like filenames with spaces. This should also be portable to other shells too.

Want to know more about bash globbibg? Check out


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s