tested on debian:13 - version 5.2.37 MacOs - version 3.2.57
offical documentation: https://www.gnu.org/software/bash/manual/html_node/index.html
rules of thumbs google style: https://google.github.io/styleguide/shellguide.html more info: https://github.com/awesome-lists/awesome-bash
general decision in bash
- unguarded bash file = one function
- bash usually should have main and functions
- bash file should be independant
- source is used when configuration is required, or overwrite is needed. otherwise prefer a seperate call
- add testing to your files, example framework bat-core
- use shellcheck
sh generic guard
if (return 2>/dev/null); then
# this file is called directly
fiBash specific guard
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi