1

I am using bash on MacBook Air running macOS Mojave and wanted to change the color of terminal prompt.

I was helped on this question on SO and this IBM blog after which I used

export PS1="\[\e[30;47m\]\W\[\e[30;47m\]$\[\e[0m\]"

which has colour, wrapping, working directory and $.

It changed the colour but the prompt doesn't have (base) or (ml) in it. It is the virtual environment I created using conda. conda deactivate removes it. Originally,

$ echo $PS1
(base) \h:\W \u\$           #original prompt with base.

$ conda deactivate          
$ echo $PS1
\h:\W \u\$                  #original prompt with no environment.  

$ conda activate ml
$ echo $PS1
(ml) \h:\W \u\$             #original prompt with ml environment. 

$ export PS1="\[\e[30;47m\]\W\[\e[30;47m\]$\[\e[0m\]"
$ echo $PS1
\[\e[30;47m\]\W\[\e[30;47m\]$\[\e[0m\]    #new prompt with no environment 
                                          #while ml is active   

What can I include to have the (base) or (ml) there, to make it like

$ echo $PS1
(base) \[\e[30;47m\]\W\[\e[30;47m\]$\[\e[0m\]    #expected result.

I didn't find any reference on bash manual for virtual environment under controlling the prompt section.

I need to find where the current active environment is. Either in any file or any command that invokes the env name, which can written in ~/.bash_profile and then add that variable to PS1.

4
  • Virtual environments aren't a bash thing, no wonder you won't find anything in the man page.
    – nohillside
    Commented Aug 22, 2019 at 19:40
  • @nohillside Can I have the stored variable for the current active env in the bash_profile file somehow? and then add it to PS1?
    – anki
    Commented Aug 22, 2019 at 19:41
  • Not sure what you want. Doesn't conda set that automatically once you activate/create an environment?
    – nohillside
    Commented Aug 22, 2019 at 19:58
  • @nohillside But it is not in PS1 after modification. I want to see which env is active. See expected vs actual result.
    – anki
    Commented Aug 22, 2019 at 20:07

1 Answer 1

0

You can activate a virtual environment for conda by running

conda activate base

Replace base with whatever (defined) conda environment you want to use.

pse@Mithos:~$ export PS1="\[\e[30;47m\]\W\[\e[30;47m\]$\[\e[0m\]"
~$cd .tmp
/Users/pse/.tmp
.tmp$conda activate base
((base) .tmp$conda deactivate 
.tmp$
4
  • Ah after activation, it does show the active env in the modified prompt too! Also just realised that by default, base is active, even after conda deactivate. I was just worried that I should not be working in a different one. Also, conda info --envs was helpful. docs.conda.io/projects/conda/en/latest/user-guide/tasks/…
    – anki
    Commented Aug 23, 2019 at 6:43
  • Can conda info --envs be run at terminal startup automatically? I want to know if ~/.bash_profile can do that or not.
    – anki
    Commented Aug 23, 2019 at 6:44
  • @ankiiiiiii Why shouldn't you be able to run it from .bash_profile?
    – nohillside
    Commented Aug 23, 2019 at 12:29
  • because I didnt know it could be done. But now I do, tested this morning. :) Thanks
    – anki
    Commented Aug 23, 2019 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .