To prevent a child process (like srun --pty bash or any subshell) from inheriting the environment variable set in the parent shell, you have a few options:
Unset the variable in the parent shell before launching the child process:
bashexport B=C unset B srun --pty bash -c 'echo $B' # Will not see C
But this removes B for all subsequent commands in the parent shell.
env -i to Start a Clean EnvironmentLaunch the child process with a clean environment, then explicitly set only the variables you want:
bashexport B=C env -i srun --pty bash -c 'echo $B' # Will not see C
This starts the child process with an empty environment.
Unset the variable in the child process itself:
bashexport B=C srun --pty bash -c 'unset B; echo $B' # Will not see C
srun --export=NONE (Slurm-specific)If you are using Slurm, you can prevent environment propagation with --export=NONE:
bashexport B=C srun --export=NONE --pty bash -c 'echo $B' # Will not see C
This tells Slurm not to export any environment variables to the child process.
envRun the command in a subshell with a modified environment:
bashexport B=C (env -u B srun --pty bash -c 'echo $B') # Will not see C
This unsets B only for the subshell.
Summary:
The most robust and Slurm-specific way is to use srun --export=NONE. For general shell use, env -i or unsetting the variable in the child process are effective.