From the course: Bash Patterns and Regular Expressions

What are globs?

- There are various types of pattern matching that can be done in Bash. There are globs, extended globs, brace expansion, and two types of regular expressions, basic and extended. The first type of pattern matching we'll talk about is file globbing. Originally, in Bell Labs' Unix, there was a command called glob that expanded wildcard characters into a list of unquoted arguments. Globbing is now a function built into the shell. This is important to note, as different shells may handle globs differently. There are also bash shell options that affect how globs work. We'll talk more about these in later videos. Since this course is about bash pattern matching, we'll focus on bash globs. In this sense, globs are a set of bash features that match or expand certain types of patterns. Globs can look and act like regular expressions but are less expressive and are easier to use. They're also easier for the system to process, as well. Do not confuse globs and regular expressions. Globs match file names, whereas regular expressions match text. Sometimes this functionality can look blurred, depending on how you're using it. We can use the ls command to get a list of files in a directory using a glob. This example would get all files starting with the numbers zero through nine, followed by one character of any type, and then the word file followed by any number of characters of any type, and finally ended with .txt. For instance, if there were three files in the directory named 1_file-rev1.txt, 2_file-rev1.txt, and 3_file-rev1.txt, the glob would expand to include these, and the resulting list would be handed to the ls command as if we typed it in. It's important to understand that this process is being handled by the shell, and the ls command is unaware of it. To use a regular expression to do the same job, we need to use a command that has regular expression support built in, such as grep. Note that ls does not support regular expressions. We'd still list the files in the directory with the ls command, and then pipe the resulting text to grep, which would then use a regular expression to match the text. Just remember that grep is matching text and ls, in the previous example, was matching files. To make matters more confusing, grep could use globs and regular expressions in the same line. In this case, the shell interprets the *.txt glob and expands it into a list of all files ending with .txt first, in my example, file1.txt and file2.txt. It then passes this list to grep, and grep searches inside the files using a regular expression for any lines that start with a capital A, have any number of characters of any type, and end with .txt. Notice that the dot has to be escaped, since a dot in a regular expression has special meaning. It's very important that we acknowledge that bash processes the glob and passes these files to the grep command as arguments. To keep the shell from interpreting the regular expression as a glob, we enclose the regular expression in single quotes.

Contents