3 ms·
Also, "unofficial bash strict mode" is terrific, especially if you intend to distribute a script: http://redsymbol.net/articles/unofficial-bash-strict-mode/ ht
by jamiesonbecker 6y ago
Also, "unofficial bash strict mode" is terrific, especially if you intend to distribute a script:
http://redsymbol.net/articles/unofficial-bash-strict-mode/ http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
(You can always turn, for example, error checking back off for an individual portion of the script with `set +e`)
- m3adow 6y agoAdding "-E" should be considered as well, so traps are also applied in functions.
- jonahx 6y agoThe options are good, but I don't like the IFS suggestion. Bash has a perfectly good way of dealing with spaces in array elements. Fixing the example in the article is as simple as adding double quotes around the array: for name in "${names[@]}"; do echo "$name" done The will not result in a single element, but will destructure each element of names, as if each were double-quoted, and will print the same output that the "strict mode IFS setting" does. What's more, the double-quoted array solution will work even if the elements contain newlines or tabs.