24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

posix<br />

off<br />

privileged<br />

off<br />

verbose<br />

off<br />

vi<br />

off<br />

xtrace<br />

off<br />

See the <strong>Bash</strong> Info pages, section Shell Built-in Commands->The Set Built-in for a description of each option.<br />

A lot of options have one-character shorthands: the xtrace option, for instance, is equal to specifying set<br />

-x.<br />

3.6.2. Changing options<br />

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Shell options can either be set different from the default upon calling the shell, or be set during shell<br />

operation. They may also be included in the shell resource configuration files.<br />

The following command executes a script in POSIX-compatible mode:<br />

willy:~/scripts> bash --posix script.sh<br />

For changing the current environment temporarily, or for use in a script, we would rather use set. Use - (dash)<br />

for enabling an option, + for disabling:<br />

willy:~/test> set -o noclobber<br />

willy:~/test> touch test<br />

willy:~/test> date > test<br />

bash: test: cannot overwrite existing file<br />

willy:~/test> set +o noclobber<br />

willy:~/test> date > test<br />

The above example demonstrates the noclobber option, which prevents existing files from being<br />

overwritten by redirection operations. The same goes for one-character options, for instance -u, which will<br />

treat unset variables as an error when set, and exits a non-interactive shell upon encountering such errors:<br />

willy:~> echo $VAR<br />

willy:~> set -u<br />

willy:~> echo $VAR<br />

bash: VAR: unbound variable<br />

This option is also useful for detecting incorrect content assignment to variables: the same error will also<br />

occur, for instance, when assigning a character string to a variable that was declared explicitly as one holding<br />

only integer values.<br />

One last example follows, demonstrating the noglob option, which prevents special characters from being<br />

expanded:<br />

willy:~/testdir> set -o noglob<br />

willy:~/testdir> touch *<br />

willy:~/testdir> ls -l *<br />

-rw-rw-r-- 1 willy willy 0 Feb 27 13:37 *<br />

Chapter 3. The <strong>Bash</strong> environment 54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!