Disabling globbing, and other options

As powerful as globbing is, this is also what makes it dangerous. For that reason, you might want to take drastic measures and turn globbing off. While this is possible, we have not seen it in practice. However, for some work or scripts, turning off globbing might be a good safeguard.

Using the set command, we can, as the man page states, change the value of a shell option. In this case, using -f will turn off globbing, as we can see when we try to repeat our previous example:

reader@ubuntu:/tmp$ cat p?ng
ping
pong
reader@ubuntu:/tmp$ set -f
reader@ubuntu:/tmp$ cat p?ng
cat: 'p?ng': No such file or directory
reader@ubuntu:/tmp$ set +f
reader@ubuntu:/tmp$ cat p?ng
ping
pong

Options are turned off by prefixing a minus (-), and turned on by prefixing a plus (+). As you might remember, this is not the first time you're using this functionality. When we debugged our Bash scripts, we started those not with bash, but with bash -x.

In this case, the Bash subshell executes a set -x command before calling the scripts. If you use set -x in your current terminal, your commands would start to look like this:

reader@ubuntu:/tmp$ cat p?ng
ping
pong
reader@ubuntu:/tmp$ set -x
reader@ubuntu:/tmp$ cat p?ng
+ cat ping pong
ping
pong
reader@ubuntu:/tmp$ set +x
+ set +x
reader@ubuntu:/tmp$ cat p?ng
ping
pong

Note that we can now see how the globbing pattern is resolved: from cat p?ng to cat ping pong. Try to remember this functionality; if you're ever at the point of pulling your hair out because you have no idea why a script isn't doing what you want, a simple set -x might make all the difference! And if it doesn't, you can always revert to normal behavior with set +x, as shown in the example.

set has many interesting flags that can make your life easier. To see an overview of the capabilities of set in your Bash version, use the help set command. Because set is a shell builtin (which you can verify with type set), looking for a man page with man set does not work, unfortunately.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.206.219