Make sure failure in the middle of a pipe is caught

Normally, the success or failure of a pipeline is decided by the last command.
Cleanup and make code more consistent

- Use bash's extended test command - [[ ... ]]
- Quote some unquoted $ signs
- Use local instead of global in load_env_file