This page is READ-ONLY. It is generated from the old site.
All timestamps are relative to 2013 (when this page is generated).
If you are looking for TeX support, please go to

Bash: some useful "set"

Added by ruby 11 months ago  »  Votes: 2/2

When using Bash, you can change the default behavior of the script/shell by set command. Example

  1. set -e: The script/shell will exit immediately if a simple command exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return status is being inverted using !.
  2. set -C: Prevent output redirection using >, >&, and from overwriting existing files. You need to use, for e.g, tee -a, if you really want to overwrite some file.
  3. set -u: Treat unset variables as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.
  4. set -x: Print a trace of simple commands and their arguments after they are expanded and before they are executed. This is very useful when debugging your script, though it is not really helpful in many cases.

set -u is very useful and it's highly recommended when your script/shell is running as root. For example, if you invoke rm -rfv /$_MY_DIR/etc/ and if the variable $_MY_DIR isn't defined (unset), your script is simply executed as rm -rfv /etc/ -- this is dangerous as that means that you delete all configuration on your server. Please note that, an unset variable isn't an empty variable. (A defined variable is DEFINED but it may as an EMPTY VALUE.)