Globstar: New Bash Globbing Option
In shell-speak, globbing is what the shell does when you use a wildcard in a command (e.g. * or ?). Globbing is matching the wildcard pattern and returning the file and directory names that match and then replacing the wildcard pattern in the command with the matched items. Bash version 4.0 adds a new globbing option called globstar which treats the pattern ** differently when it's set.
Normally, if you use the pattern ** on the command line it acts the same as the * pattern. For example, given the following directory:
$ ls -l total 8 -rw-r--r-- 1 mitch users 0 2010-05-05 10:36 a -rw-r--r-- 1 mitch users 0 2010-05-05 10:36 b drwxr-xr-x 2 mitch users 4096 2010-05-05 10:36 c drwxr-xr-x 2 mitch users 4096 2010-05-05 10:36 d
if you execute the following commands:
$ for i in ** > do > echo $i > done
you get the following output:
a b c d
If you now set the globstar option with the following command:
$ shopt -s globstar
and then re-run the for loop from above, you now get:
a b c c/e d d/f
With globstar set, bash recurses all the directories. Note that this will recurse all directory levels, not just one level.
You can see if globstar is set by issuing the shopt command without any arguments:
$ shopt ... force_fignore on globstar on gnu_errfmt off ...
or if you want to test it in a script you can use the -q option to shopt and test the exit status, for example:
if shopt -q globstar; then ... if
Mitch Frazier is an Associate Editor for Linux Journal.
Webinar: 8 Signs You’re Beyond Cron
On Demand NOW
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.View Now!
|diff -u: What's New in Kernel Development||May 06, 2015|
|Chrome-Colored Parakeets||May 05, 2015|
|Mumblehard--Let's End Its Five-Year Reign||May 04, 2015|
|An Easy Way to Pay for Journalism, Music and Everything Else We Like||May 04, 2015|
|When Official Debian Support Ends, Who Will Save You?||May 01, 2015|
|May 2015 Issue of Linux Journal: Cool Projects||May 01, 2015|
- diff -u: What's New in Kernel Development
- Mumblehard--Let's End Its Five-Year Reign
- Chrome-Colored Parakeets
- When Official Debian Support Ends, Who Will Save You?
- An Easy Way to Pay for Journalism, Music and Everything Else We Like
- Ubuntu Ditches Upstart
- "No Reboot" Kernel Patching - And Why You Should Care
- Picking Out the Nouns
- Return of the Mac
- DevOps: Better Than the Sum of Its Parts