I'm trying to display path in colors but I have problem with this.
I have some constant path - in variable MyPath. I want to create colorized path in PS1 but if pwd showing different path then my (but including MyPath) then the rest I want to print in different color (with slash in different color).
I wrote some code but I don't know hot to apply this to PS1.
It should be something like this :
[ root@192.168.1.199:/ -> /media/user/folder/ ]
# : cd /var/www/html
[ root@192.168.1.199:/ -> /var/www/html/ ] (blue slash and green dir names)
# : cd applications
[ root@192.168.1.199:/ -> /var/www/html/applications/ ] (blue slash and green dir names but last 2 slashes in green color and last dir "application" in red color)
# : cd tmp
[ root@192.168.1.199:/ -> /var/www/html/applications/tmp/ ] (blue slash and green dir names but last 3 slashes in green color and 2 last dirs "application" and "tmp" in red color)
And I got stuck - I don't know how to do this.
My code :
#!/bin/bash
MyPath="/var/www/html"
MyPathLength=$( echo ${MyPath} | wc -m)
CurrentPath="/var/www/html/functions/design"
slashColor="\[$(tput setaf 6)\]/\[$(tput sgr0)\]"
dirColor="\[$(tput setaf 2)\]"
path="";
FinalPath="";
for w in $(echo ${CurrentPath} | tr "/" " ");
do
path="${path}/${w}";
pathLength=$( echo ${path} | wc -m)
if [ "${pathLength}" == "${MyPathLength}" ];
then
FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]\[$(tput setaf 6)\]/\[$(tput sgr0)\]";
elif [ "${pathLength}" -lt "${MyPathLength}" ];
then
FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]";
elif [ "${pathLength}" -gt "${MyPathLength}" ];
then
FinalPath="${FinalPath}\[$(tput setaf 1)\]$w\[$(tput sgr0)\]\[$(tput setaf 2)\]/\[$(tput sgr0)\]";
fi;
done
echo "PS1=\"${FinalPath}\"" > /home/bashrc_split
cd "/";
(
bash --rcfile /home/bashrc_split # I want to open new shell with new PS1
)
Any help?