4 ms·
I use this holy trinity in most of my scripts: set -o nounset # set -u set -o errexit # set -e set -o pipefail # I'm not sure this has a short form. I use
by oblio 12y ago
I use this holy trinity in most of my scripts:
set -o nounset # set -u
set -o errexit # set -e
set -o pipefail # I'm not sure this has a short form.
I use the long forms for the option names because they're self documenting.
If anyone has more suggestions, I'm all ears.
- drothlis 12y agoDavid A. Wheeler recommends setting IFS=$'\n\t' to handle spaces in filenames & variables. http://www.dwheeler.com/essays/filenames-in-shell.html http://www.dwheeler.com/essays/filenames-in-shell.html
- kazinator 12y agoAll those rules add up to: "avoiding writing anything complicated or general purpose in the shell language that is intended to be used by any user over any set of files; stick to handling known input materials which are closely associated with the script."
- ufo 12y agoAre these bash-exclusive or do they also work on posix-like sh?
- oblio 12y agoI doubt it. set -e is POSIX, I think, but the rest are probably bash extensions. I'll probably get flak for this, but for 99% of the scripts, at this point I'd just use bash. Linux has it as a default, as does Mac OS, anyone running BSDs can install it trivially, and Unixes probably have it as well since most of them have antiquated CLI environments and administrators usually install GNU utils to have a more human working environment.
- kazinator 12y agoSee here: http://pubs.opengroup.org/onlinepubs/009695399/utilities/set.html http://pubs.opengroup.org/onlinepubs/009695399/utilities/set... "-u The shell shall write a message to standard error when it tries to expand a variable that is not set and immediately exit. An interactive shell shall not exit."