0

I have two storage drives and am trying to configure my bashrc file to do this:

If the cwd is on drive B :

  • Truncate the bash prompt to begin at the /home directory of the second drive
  • Change the color of bash prompt to red to know I am working in the other drive.

So far I have been able to accomplish changing the color with the following code and can truncate the entire prompt to only show the basename by using the '\W' modifier -- but am struggling with how to trim the PROMPT_COMMAND by specifying a bottom directory...

i.e.

/media/devj/2a24a03f-99a1-44bd-9a53-341zdd68334b/home/dev --> /home/dev/

Solution: Thanks for the help, this is what I ended up doing:

#Check if we are accessing my home folder from secondary drive
#If so change the color and trim the filepath to secondary /home
bash_prompt_command() {
    root_path=`pwd | awk -F/ '{print $(NF-(NF-2))}'`
    if [ $root_path = media ]; then #Shared drive
        #Trim path up to the home directory
        trim_path=${PWD#/media/devj/9a84a09e-80e9-44bd-9a53-342e3d48334c/}
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$trim_path\[\033[38;5;214m\]$(parse_git_branch)\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[38;5;214m\]$(parse_git_branch)\[\033[00m\]\$ '
    fi
}

# init it by setting PROMPT_COMMAND
PROMPT_COMMAND=bash_prompt_command
Anon.
  • 85
  • 5
  • 1
    [`PROMPT_DIRTRIM`](https://www.gnu.org/software/bash/manual/bash.html#index-PROMPT_005fDIRTRIM) might be useful here. – Benjamin W. Dec 01 '19 at 22:47
  • What do you expect the prompt to look like when you're in some subdirectory X of drive A? And what do you expect it to look like when you're in some subdirectory Y of drive B? – root Dec 01 '19 at 23:07
  • Thanks for the tip of PROMPT_DIRTRIM, exactly what I was looking for. Is there a way to check how deep the current working directory is from root so I can dynamically change the value of PROMPT_DIRTRIM. Basically I want drive a and drive b to look the same starting at the home directory within each drive. I want it to look like this if I am in my documents folder on drive B. /dev/home/Documents – Anon. Dec 01 '19 at 23:14
  • 1
    To see how deep you are, you could count slashes in the path to the current directory in your `bash_prompt_command`: `slashes=${PWD//[!\/]}; depth=${#slashes}` and then set `PROMPT_DIRTRIM` to something based on that. – Benjamin W. Dec 01 '19 at 23:59
  • @BenjaminW. I thought about doing that but ended up using this method instead. `trim_path=$(pwd) trim_path=${trim_path#/media/devj/9a84a09e-80e9-44bd-9a53-342e3d48334c/}` – Anon. Dec 02 '19 at 00:59
  • 1
    You could shorten that to `trim_path=${PWD#/media/devj/9a84a09e-80e9-44bd-9a53-342e3d48334c/}` – Benjamin W. Dec 02 '19 at 01:03

1 Answers1

0

trim_path=${PWD#/*/*/*/}

Substitute however many directories you want to omit from the beginning of path.

Anon.
  • 85
  • 5