Skip to content

Commit 3716f43

Browse files
authored
Merge pull request #4685 from Micket/fix_unset
Use more robust mechanism for unsetting environment variables
2 parents 2ebaa47 + 46102d3 commit 3716f43

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

easybuild/tools/run.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,19 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
234234
with open(env_fp, 'w') as fid:
235235
# unset all environment variables in current environment first to start from a clean slate;
236236
# we need to be careful to filter out functions definitions, so first undefine those
237-
fid.write("unset -f $(env | grep '%=' | cut -f1 -d'%' | sed 's/BASH_FUNC_//g')\n")
238-
fid.write("unset $(env | cut -f1 -d=)\n")
237+
fid.write('\n'.join([
238+
'for var in $(compgen -e); do',
239+
' unset "$var"',
240+
'done',
241+
]) + '\n')
242+
# also unset any bash functions
243+
fid.write('\n'.join([
244+
'for func in $(compgen -A function); do',
245+
' if [[ $func != _* ]]; then',
246+
' unset -f "$func"',
247+
' fi',
248+
'done',
249+
]) + '\n')
239250

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

test/framework/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ def test_run_shell_cmd_env(self):
294294
env_script = os.path.join(cmd_tmpdir, 'env.sh')
295295
self.assertExists(env_script)
296296
env_script_txt = read_file(env_script)
297-
self.assertTrue(env_script_txt.startswith('unset -f $('))
297+
self.assertIn('unset "$var"', env_script_txt)
298+
self.assertIn('unset -f "$func"', env_script_txt)
298299
self.assertIn('\nexport FOOBAR=foobar\nexport PATH', env_script_txt)
299300

300301
cmd_script = os.path.join(cmd_tmpdir, 'cmd.sh')

0 commit comments

Comments
 (0)