3.6. More Bash options

3.6.1. Displaying options

We already discussed a couple of Bash options that are useful for debugging your scripts. In this section, we will take a more in-depth view of the Bash options.

Use the -o option to set to display all shell options:


willy:~> set -o allexport off braceexpand on emacs on errexit off hashall on histexpand on history on ignoreeof off interactive-comments on keyword off monitor on noclobber off noexec off noglob off nolog off notify off nounset off onecmd off physical off posix off privileged off verbose off vi off xtrace off 

See the Bash Info pages, section Shell Built-in Commands->The Set Built-in for a description of each option. A lot of options have one-character shorthands: the xtrace option, for instance, is equal to specifying set -x.

3.6.2. Changing options

Shell options can either be set different from the default upon calling the shell, or be set during shell operation. They may also be included in the shell resource configuration files.

The following command executes a script in POSIX-compatible mode:


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

For changing the current environment temporarily, or for use in a script, we would rather use set. Use - (dash) for enabling an option, + for disabling:


willy:~/test> set -o noclobber willy:~/test> touch test willy:~/test> date > test bash: test: cannot overwrite existing file willy:~/test> set +o noclobber willy:~/test> date > test 

The above example demonstrates the noclobber option, which prevents existing files from being overwritten by redirection operations. The same goes for one-character options, for instance -u, which will treat unset variables as an error when set, and exits a non-interactive shell upon encountering such errors:


willy:~> echo $VAR willy:~> set -u willy:~> echo $VAR bash: VAR: unbound variable 

This option is also useful for detecting incorrect content assignment to variables: the same error will also occur, for instance, when assigning a character string to a variable that was declared explicitly as one holding only integer values.

One last example follows, demonstrating the noglob option, which prevents special characters from being expanded:


willy:~/testdir> set -o noglob willy:~/testdir> touch * willy:~/testdir> ls -l * -rw-rw-r-- 1 willy willy 0 Feb 27 13:37 app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html chap_12.html f32.html glossary.html gloss.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_05.html sect_02_06.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_03_07.html sect_03_08.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_04.html sect_07_05.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_04.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_09_07.html sect_09_08.html sect_09_09.html sect_10_01.html sect_10_02.html sect_10_03.html sect_10_04.html sect_10_05.html sect_11_01.html sect_11_02.html sect_11_03.html sect_11_04.html sect_12_01.html sect_12_02.html sect_12_03.html sect_12_04.html x7253.html x7379.html