Globignore example in Bash | Linux

Shell Scripting

GLOBIGNORE is a special variable which is used by the globbing feature of bash, to omit certain patterns from a wildcard match. The next chapter looks at wildcards in more detail. Like many shell variables which are formatted as lists (like PATH, GLOBOPTS, and others), it is a colon-separated list of regular expressions. If any filename would match a pattern in GLOBIGNORE, it is treated as if that item did not exist. For example, Figure 1 shows a directory containing a few HTML, PHP, TXT, RSS, and CSS files. If you list the files starting with a, you see the first four. However, when you set GLOBIGNORE to ignore filenames matching the pattern “*.gif,” the same command


shows only the two PHP files, not the two GIF files.

Globignore

figure 1

This is more clear with a bigger match, as shown in Figure 2:

Globignore bashfigure 2

In Figure 2, the first ls shows you what files are in the directory. Setting FOO to “*.php” means that the echo $FOO will match all of the .php files. Setting GLOBIGNORE=*.php means that echo

$FOO acts as if there were no matching files to be found — it outputs its actual, unexpanded value, which is the string “*.php“.

When you then set GLOBIGNORE=a*, echo $FOO omits the files starting with “a”, and setting

GLOBIGNORE=a*:*.php results in any file which matches any of those patterns being ignored.

This can also be useful when using source control systems. For example, Figure 3-3 shows some files that have been backed up with a “~” extension:

Globignore bash

figure 3

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.