I am faced to a problem of appearing slash / appearing double slash into PS1 when I am located on root / .

I explain more precisely, I have in .zshrc :

slash_color () { dirs | awk -F "/" ' {for (i=1; i<=NF; i++) {printf "\033[38;5;75m"$i"\033[38;5;206m" "/"}} '; }
PS1='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|$(slash_color)%F{13}|%F{7} '

The goal was to colorize each slash of the current PATH.

This way, when I go to a deeper directory, I get :

prompt colorized with slash

But a problem remains when I do : $ cd /, I get the following result, i.e a double slash representing the current path and I don't know to handle this to have only a single path :

double slash issue

If someone could help me to get only one slash when I am located on root, i.e / ?

  • though an interesting case occurs if you have a filename/ folder name with a slash in between
    – anki
    Commented Apr 18, 2020 at 18:29
  • @ankii Did you try to create such a file?
    – nohillside
    Commented Apr 18, 2020 at 19:53
  • @ankii Slash != Backslash. Try touch foo/bar :-)
    – nohillside
    Commented Apr 19, 2020 at 7:57
  • @nohillside i.sstatic.net/al3tW.png i.sstatic.net/lRRxi.png though I did that in finder. Idk about terminal
    – anki
    Commented Apr 19, 2020 at 8:37
  • 1
    @ankii If you look with Terminal you'll find the / replaced by :, so it won't be an issue for the problem here. Finder is doing some legacy HFS magic on this.
    – nohillside
    Commented Apr 19, 2020 at 8:50

1 Answer 1


Change the for loop from

for (i=1; i<=NF; i++) \
 printf blue $i pink "/"; \
printf "\n"


for (i=1; i<NF; i++) \
 printf blue $i pink "/"; \
printf blue $NF pink; \
printf "\n"
  • notice the change in <=<
  • Take care of semi-colons.

or use some help from searching to get an if else condition for which the pseudocode (not correct bash syntax) is:

for all i<=NF:
 if (i<NF):
   printf blue $i pink "/" ;
   printf blue $i pink ;
printf "\n";

  • @ankli thanks a lot ! I am faced now to another issue but this is another story too, so new question, to follow ...
    – user256573
    Commented Apr 18, 2020 at 19:10

You must log in to answer this question.