Announcement

Collapse
No announcement yet.

bash question - changing prompt colors based on pwd

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    bash question - changing prompt colors based on pwd

    What I want to accomplish is: changing the prompt color based on the directory I am using.

    I have several OS versions installed, and for example, if I am cd's into another OS's directory structure, working on files there, I would like the prompt to be a different color than if I am in the home OS. All of the other OS's are accessed through /media -- /media/testing, /media/aptosid, etc., so a test that looks for "/media/" would work.

    The idea here is that if I am changing files around, I can tell at a glance that I am doing it in the right place. So I don't, for example, change all of my source entries from "natty" to "oneiric" in my natty directory. You should be able to figure out from that example why I want to change the prompt colors...

    The relevant /bashrc line is this:

    if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

    but I'm not quite sure how to insert a test line in this, or even if it would work without reloading the shell.
    We only have to look at ourselves to see how intelligent life might develop into something we wouldn't want to meet. -- Stephen Hawking

    #2
    Re: bash question - changing prompt colors based on pwd

    Look into dircolors and how to edit it. You might have to experiment but maybe it will let you enter

    /media/test=1:31

    or something similar.

    Please Read Me

    Comment


      #3
      Re: bash question - changing prompt colors based on pwd

      Sorry, that doesn't work. How about nested if's?

      if [ "$color_prompt" = yes ]; then
      if [ "$PWD"="/media/testing" ]; then
      PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;31m\]\w\[\033[00m\]\$ '
      else
      PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
      fi
      else
      PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
      fi

      Still, it wouldn't take effect unless you reloaded bash right?

      EDIT: Tested and it works if you open the konsole with the --workdir /media/testing option. However, it stays the same color when you change directories which isn't what you want I think.

      Please Read Me

      Comment


        #4
        Re: bash question - changing prompt colors based on pwd

        How about an alias for "cd /media/testing" that would change the prompt? But again, you'd have to be able to change it back automatically.

        Please Read Me

        Comment


          #5
          Re: bash question - changing prompt colors based on pwd

          Thanks for your efforts and suggestions.

          I expect that a script won't work, at least not one that is part of .bashrc, because, as I understand it, .bashrc is read only when the shell is invoked, so cd'ing once the shell is started would never get picked up. I tried something like that anyway, and it didn't work.

          One could set up a script that is read every time a prompt is issued. I suspect the only way to really get it to work would be to hack dircolors, or something like that, so that it would recognize specific directories, and issue colors for them. I am going to try playing with LS_COLORS and see if I can get it to recognize directories.
          We only have to look at ourselves to see how intelligent life might develop into something we wouldn't want to meet. -- Stephen Hawking

          Comment


            #6
            I realise this questing is over 2 years old however it came up high on Google when I was looking to do the same thing. Anyway oshunluvr gave me the idea for this script:

            https://gist.github.com/Gisleburt/c560f99d9e9ada359934

            Code:
            #!/bin/bash
             
            if [[ $# -gt 0 ]]
            then
                    pushd "$1" >> /dev/null
            fi
             
            if [[ "$PWD" == *'/live/'* ]]
            then
                    # red
                    PS1='\[\e[0;31m\]\u@\h:\w\$ \[\e[m\]';
            elif [[ "$PWD" == *'/stage/'* ]]
            then
                    # green
                    PS1='\[\e[0;32m\]\u@\h:\w\$ \[\e[m\]';
            else
                    # white
                    PS1='\[\e[0;37m\]\u@\h:\w\$ \[\e[m\]';
            fi
            You can alias that to cd and it will do what you want.

            Comment


              #7
              (I didn't see this the first time round.)

              I think it's nicer to make the prompt colouring independent of the command or commands used to change directories. Bash can execute stuff while outputing the prompt, using the $() syntax. First, define a function, maybe in .bashrc, or a file sourced from it:
              Code:
              function cp
              {
                  case $PWD in 
                      /media/Oneiric*)
                          col=31;; # red
                      /media/windows*)
                          col=32;; # green
                      /media/Mint*)
                          col=36;; # cyan
                      *)
                          col=40;; # white
                  esac
                  echo -ne "\e[1;${col}m"
              }
              then include $(cp) in the setting of PS1:

              PS1='\[$(cp)\]\u@\h:\w\$ \[\e[m\]';

              I sometimes have lots of stuff in such a function, depending on the needs of the job, usually to trim the directory to the most relevant parts, to keep the prompt not too long.

              Regards, John Little
              Regards, John Little

              Comment


                #8
                I didn't realise you could have functions in PS1, that's a much more elegant solution.

                Comment

                Working...
                X