Update bash scripts to work in --posix mode

This cleans up the scripts to be a consistent coding style, removes unused code, does some optimizations, and generally makes things more standardized.