`export B=C`, then how to avoid `srun --pty bash` `echo $B` seeing `C`?
Apr 29, 12:43am

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:


1. Unset the Variable Before Running the Child Process

Unset the variable in the parent shell before launching the child process:

bash
export 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.


2. Use env -i to Start a Clean Environment

Launch the child process with a clean environment, then explicitly set only the variables you want:

bash
export B=C
env -i srun --pty bash -c 'echo $B'  # Will not see C

This starts the child process with an empty environment.


3. Unset the Variable in the Child Process

Unset the variable in the child process itself:

bash
export B=C
srun --pty bash -c 'unset B; echo $B'  # Will not see C

4. Use srun --export=NONE (Slurm-specific)

If you are using Slurm, you can prevent environment propagation with --export=NONE:

bash
export 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.


5. Use a Subshell with env

Run the command in a subshell with a modified environment:

bash
export 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.

Apr 29, 12:43am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.