When you use some commands (such as ls and git), you just need to easily traverse all directories and files, and then search for them, finally, we found a "magic" wildcard "" ** "(two asterisks). After setting the Bash globstar option, ** we can match any current directory (including subdirectories) and the files. Therefore, I learned about the globstar option. When globstar is not set, ** the wildcard function is the same as *. After globstar is set, ** the matching range is not
When you use some commands (such as ls and git), you just need to easily traverse all directories and files, and then search for them, finally, we found a "magic" wildcard "" ** "(two asterisks). After setting the Bash globstar option, ** we can match any current directory (including subdirectories) and the files. Therefore, I learned about the globstar option. When globstar is not set, ** the wildcard function is the same as *. After globstar is set, ** the matching range is different (WIDER ). Note: globstar is an option introduced only in Bash 4.0. It is not supported in earlier versions? Version.
I am also curious about glob. It is hard to explain Chinese characters. It means "Expand the wildcard character", as shown in the following English:
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.
In bash man page, the globstar description is mentioned only twice, and the same thing is said, as follows:
Pathname Expansion......* Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.......globstar If set, the pattern ** used in a pathname expansion context will match a files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
A shell script for testing and learning globstar is written as follows:
#! /Bin/bashfunction show(){ for i in ** do echo $i done} cd /root/jay/echo "------------------------"echo "disable globstar option:"# globstar is disabled by defaultshopt -u globstarshowecho "------------------------"echo "enable globstar option:"shopt -s globstarshow
Execute the shell script to test globstar and view its output results. Then, you can easily understand globstar as follows:
[root@smilejay jay]# ./test_globstar.sh------------------------disable globstar option:dir1dir2file1file2index.htmltest_shopt.sh------------------------enable globstar option:dir1dir1/file3dir2dir2/file4file1file2index.htmltest_shopt.sh
References:
Http://www.linuxjournal.com/content/globstar-new-bash-globbing-option
New feature introduced by bash4: http://wiki.bash-hackers.org/bash4
Http://wiki.bash-hackers.org/syntax/expansion/globs