Search code examples
bashshellcommand-linecondaenvironment

Error in PS1 return status when modifying the .bashrc, how to solve this?


I recently modified my .bashrc file in order to have a pretty PS1 shell like this:

┌──(0) [09:57] gianluca@ubuntu-[~]
└─$ 

The problem is that with this current configuration, the return status (the (0)) is always 0 whatever is the input command I give to the shell is, even if this command returns an error message.

The full .bashrc file is here, while the relevant lines which broken the behaviour are these:

...
# Virtualenv settings to correct env position on the shell
PS1_START="┌──"
export VIRTUAL_ENV_DISABLE_PROMPT=1
VENV="\$(virtualenv_info)";
PS1="${VENV}${PS1}"

# Conda env settings to correct env position on the shell
CENV="\$(condaenv_info)";
PS1="${CENV}${PS1}"
PS1="${PS1_START}${PS1}"
...

In which I use these functions defined in the .bash_functions dotfile:

function virtualenv_info(){
    if [[ -n "$VIRTUAL_ENV" ]]; then
        venv="${VIRTUAL_ENV##*/}"
        [[ -n "$venv" ]] && echo "($venv) "
    else
        venv=""
    fi
}

function condaenv_info(){
    if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
        venv="${CONDA_DEFAULT_ENV##*/}"
        [[ -n "$venv" ]] && echo "($venv) "
    else
        venv=""
    fi
}

I needed to add these lines in order to keep the environment name in PS1 in the right position when activated and not at the beginning of the PS1 itself.

Do you know how to help me? Thanks.


Solution

  • Ok, I finally solved by replacing this:

    ...
    # Virtualenv settings to correct env position on the shell
    PS1_START="┌──"
    export VIRTUAL_ENV_DISABLE_PROMPT=1
    VENV="\$(virtualenv_info)";
    PS1="${VENV}${PS1}"
    
    # Conda env settings to correct env position on the shell
    CENV="\$(condaenv_info)";
    PS1="${CENV}${PS1}"
    PS1="${PS1_START}${PS1}"
    ...
    

    with this:

    ...
    # Virtualenv settings to correct env position on the shell
    export VIRTUAL_ENV_DISABLE_PROMPT=1
    PS1='┌──(\[\033[36m\]$?\[\033[37m\]) $(env_info virtualenv)$(env_info condaenv)'$PS1
    ...