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:

  1. willy:~> **set \-o**
  2. allexport off
  3. braceexpand on
  4. emacs on
  5. errexit off
  6. hashall on
  7. histexpand on
  8. history on
  9. ignoreeof off
  10. interactive-comments on
  11. keyword off
  12. monitor on
  13. noclobber off
  14. noexec off
  15. noglob off
  16. nolog off
  17. notify off
  18. nounset off
  19. onecmd off
  20. physical off
  21. posix off
  22. privileged off
  23. verbose off
  24. vi off
  25. xtrace off

See the Bash Info pages, section -> 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:

  1. 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:

  1. willy:~/test> **set \-o _noclobber_**
  2. willy:~/test> **touch test**
  3. willy:~/test> **date > test**
  4. bash: test: cannot overwrite existing file
  5. willy:~/test> **set +o _noclobber_**
  6. 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:

  1. willy:~> **echo $VAR**
  2. willy:~> **set \-u**
  3. willy:~> **echo $VAR**
  4. 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:

  1. willy:~/testdir> **set \-o _noglob_**
  2. willy:~/testdir> **touch \***
  3. willy:~/testdir> **ls \-l \***
  4. -rw-rw-r-- 1 willy willy 0 Feb 27 13:37 \*

https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_06.html#/