Use the globstart option in Bash in shell

Source: Internet
Author: User
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/bash
function 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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.