The Unix shell uses the Globstart option in Bash to use the tutorial

Source: Internet
Author: User

When you are using some commands, such as: LS, git, just met some requirements is to easily traverse all the directories and files, and then after the search, finally found a "magic" wildcard "* *" (two asterisks), after setting the bash globstar option, * * You can match any current directory (including subdirectories) and the files in it. So, to understand the Globstar this option, when not set Globstar, * * Wildcard function and * is the same, and set the Globstar, * * Matching range is different (a wider). Note: Globstar is an option that was introduced in Bash 4.0, and previous versions are not supported and use "bash–version" to produce the version of Bash currently in use.

About the word glob, I also feel curious, Chinese is not easy to explain, is roughly the "spread of the wildcard" meaning, such as the following English bar:

In Shell-speak, globbing are what the shell does when your 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 Wil Dcard the command with the matched items.

In Bash's man page, the description of Globstar mentioned only two times, saying the same thing, as follows:

The code is as follows Copy Code
Pathname expansion
......
* matches any string, including the null string. When the Globstar shell option was enabled, and * is
Used in a pathname expansion context, two adjacent *s used as a I-match all files
and zero or more directories and subdirectories. If followed by A/, two adjacent *s'll match only
Directories and subdirectories.
......
Globstar
If set, the pattern * * used in a pathname expansion context would match a files and zero or more directories
and subdirectories. The If is followed by A/, only directories and subdirectories match.



Write a test and learn the Globstar shell script as follows:

The code is as follows Copy Code
#!/bin/bash
<pre lang= "Bash" >
Function Show ()
{
For i in * *
Todo
Echo $i
Done
}

cd/root/jay/
echo "------------------------"
echo "Disable Globstar option:"
# Globstar is disabled by default
Shopt-u Globstar
Show
echo "------------------------"
echo "Enable Globstar option:"
Shopt-s Globstar
Show

Executing the shell script of the test Globstar above, looking at its output, it is easy to understand Globstar, as follows:

[Root@smilejay jay]#./test_globstar.sh
------------------------
Disable Globstar option:
Dir1
Dir2
File1
File2
Index.html
test_shopt.sh
------------------------
Enable Globstar option:
Dir1
Dir1/file3
Dir2
Dir2/file4
File1
File2
Index.html
test_shopt.sh
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.