Home > Enterprise >  Clean environment for bash shell
Clean environment for bash shell

Time:11-09

Is there a way to tell a bash script not to import any variables from the parent shell i.e. ignore exported variables. There is such capability in slurm --export=NONE so I wonder if there is an option I can put in the #!/bin/bash line to get similar behavior.

CodePudding user response:

On linux the cleanest option I found was:

#!/usr/bin/env -S - bash
env

which for me prints:

PWD=/home/allan
SHLVL=1
_=/usr/bin/env

Another option is:

#!/usr/bin/env bash
[ -n "$HOME" ] && exec -c "$0"
env

Possible using $BASH_SOURCE[0] instead of $0 as the latter can be set by user. $BASH_SOURCE, however, is not always set. Hard-coding the script path would work but that's ugly.

  • Related