Skip to content

Commit ee9f6ce

Browse files
committed
Use more robust mechanism for unsetting environment variables
Parsing the output of "env" can be fragile for certain complex environment. Using compgen should be safer.
1 parent 8fc631d commit ee9f6ce

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

easybuild/tools/run.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,19 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
217217
with open(env_fp, 'w') as fid:
218218
# unset all environment variables in current environment first to start from a clean slate;
219219
# we need to be careful to filter out functions definitions, so first undefine those
220-
fid.write("unset -f $(env | grep '%=' | cut -f1 -d'%' | sed 's/BASH_FUNC_//g')\n")
221-
fid.write("unset $(env | cut -f1 -d=)\n")
220+
fid.write('\n'.join([
221+
'for var in $(compgen -e); do',
222+
' unset "$var"',
223+
'done',
224+
]) + '\n')
225+
# also unset any bash functions
226+
fid.write('\n'.join([
227+
'for func in $(compgen -A function); do',
228+
' if [[ $func != _* ]]; then',
229+
' unset -f "$func"',
230+
' fi',
231+
'done',
232+
]) + '\n')
222233

223234
# excludes bash functions (environment variables ending with %)
224235
fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())

0 commit comments

Comments
 (0)