0

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 :

setopt PROMPT_SUBST
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 / ?

5
  • though an interesting case occurs if you have a filename/ folder name with a slash in between – anki Apr 18 '20 at 18:29
  • @ankii Did you try to create such a file? – nohillside Apr 18 '20 at 19:53
  • @ankii Slash != Backslash. Try touch foo/bar :-) – nohillside Apr 19 '20 at 7:57
  • @nohillside i.stack.imgur.com/al3tW.png i.stack.imgur.com/lRRxi.png though I did that in finder. Idk about terminal – anki Apr 19 '20 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 Apr 19 '20 at 8:50
1

Change the for loop from

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

to

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 "/" ;
 else:
   printf blue $i pink ;
printf "\n";

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

You must log in to answer this question.

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