I change `PS1` in `~/.bashrc`, but `apptainer run my_debian.sif` does not take this changed `PS1` into account, while it takes into account the remaining of the `~/.bashrc`, why?
2/2
May 20, 12:11pm

The behavior you're observing with apptainer run and your custom PS1 in ~/.bashrc is likely due to how Apptainer (formerly Singularity) handles shell initialization and the type of shell it invokes by default.

Here’s why this happens and how to fix it:


Why PS1 is Not Applied

  1. Non-Interactive Shell by Default When you run apptainer run my_debian.sif, Apptainer typically starts a non-interactive, non-login shell inside the container. In such shells, ~/.bashrc is not automatically sourced. However, some parts of your ~/.bashrc (e.g., aliases, functions, or environment variables) may still be applied if they are set in a way that Bash reads them even in non-interactive mode (e.g., via BASH_ENV or explicitly sourced).

  2. PS1 is for Interactive Shells The PS1 variable (your shell prompt) is only used by interactive shells. Since Apptainer’s default shell is non-interactive, PS1 is ignored.

  3. Login vs. Non-Login Shells

    • If you use apptainer shell my_debian.sif, it starts an interactive shell, and ~/.bashrc (and thus PS1) will be applied.
    • apptainer run does not start an interactive shell by default, so PS1 is not loaded.

How to Fix It

Option 1: Force an Interactive Shell

If you want your custom PS1 to appear, use apptainer shell instead of apptainer run:

bash
apptainer shell my_debian.sif

This will give you an interactive shell inside the container, and ~/.bashrc (including PS1) will be sourced.


Option 2: Explicitly Source ~/.bashrc in apptainer run

If you must use apptainer run, you can force Bash to source ~/.bashrc by passing a command that starts an interactive shell:

bash
apptainer run --shell /bin/bash my_debian.sif

Or, explicitly source it in your command:

bash
apptainer exec my_debian.sif bash -c "source ~/.bashrc; your_command"

Option 3: Set PS1 in a File Sourced by Non-Interactive Shells

If you want PS1 to work even in non-interactive shells (uncommon, but possible), you can set it in a file that Bash reads for non-interactive shells, such as:

  • The BASH_ENV environment variable (point it to a file containing PS1).
  • The --nv flag in Apptainer to pass your host’s environment.

Example:

bash
export BASH_ENV=~/.bashrc
apptainer run my_debian.sif

But this is not recommended, as PS1 is meant for interactive use.


Summary Table

apptainer run
Non-interactive
No (usually)
❌ No
apptainer shell
Interactive
Yes
✅ Yes
apptainer exec bash
Interactive
Yes
✅ Yes

Key Takeaway: PS1 is for interactive shells. Use apptainer shell or explicitly start an interactive shell if you want your custom prompt. For apptainer run, the shell is non-interactive by design, so PS1 is ignored.

May 20, 12:11pm

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

Vibe can make mistakes. Check answers. Learn more