From 4b14a6eaa8412f56020c70e4d87ec35c7e9a098e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20=C3=96hman?= Date: Wed, 16 Oct 2024 15:20:02 +0200 Subject: [PATCH] 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. --- easybuild/tools/run.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index d49258a48a..eb9a94fc2e 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -217,8 +217,10 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file): with open(env_fp, 'w') as fid: # unset all environment variables in current environment first to start from a clean slate; # we need to be careful to filter out functions definitions, so first undefine those - fid.write("unset -f $(env | grep '%=' | cut -f1 -d'%' | sed 's/BASH_FUNC_//g')\n") - fid.write("unset $(env | cut -f1 -d=)\n") + fid.write("for var in $(compgen -e); do\n unset \"$var\"\ndone\n") + # also unset any bash functions + fid.write("for func in $(compgen -A function); do\n if [[ $func != _* ]]; then\n" + " unset -f \"$func\"\n fi\ndone\n") # excludes bash functions (environment variables ending with %) fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())