Sanitize environment with command or bash script?

You can use env and a wrapper script:

#!/bin/bash
env -i /path/to/main_script.sh

From man env:

   -i, --ignore-environment
          start with an empty environment

You can also, of course, just run the script as env -i script.sh if you are running it by hand. Unfortunately as far as I can tell one can’t use the script shebang to run bash through env like this; the shebang can only accept two parameters by definition as parsed by the kernel.

The other semi-reliable solution using env or exec -c (which does pretty much the same) that I can think of would be to use exec -c $0 to re-run the script with a clean environment if you detect it’s not clean. Assuming $HOME is set in an unclean environment and is not set in a clean one (that’s true in my install):

#!/bin/bash
[ "$HOME" != "" ] && exec -c $0
# rest of the script here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)