Search code examples
awkzshmacos-catalinazshrcdebian-buster

awk: runtime error - Compatility between zsh config on MacOS Catalina and zsh config on Debian Buster


I have a zsh config on MacOS Catalina which works well. No, I would like to get the same but for Debian 10 Buster.

The issue occurs in the using of function for PROMPT that displays pink slashes that separate the PATH current working where directories are in blue.

On MacOS, I do it like this (into my .zshrc) :

# Path with colorized forward slash
slash_color() {
dirs | awk -F "/" '{ blue="%{\033[38;5;75m%}"; \
                     pink="%{\033[38;5;206m%}"; \
                     for (i=1; i<NF; i++) \
                      printf blue $i pink "/"; \
                     printf blue $NF pink; \
                   }';
}

# Prompt final
PROMPT=$'%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|$(slash_color)%F{13}|%F{7} '

The result looks like for PROMPT :

zsh prompt for MacOS Catalina

Now, on Debian Buster, I have copied the ~/.zshrc from MacOS Catalina.

and when PROMPT is displayed, the PATH of current working directory is not displayed (empty) and I get the following error :

awk: run time error: not enough arguments passed to printf("%{%}~%{%}/")
    FILENAME="-" FNR=1 NR=1

I don't know why I have this error on Debian and not on MacOS. I suspect this is due to a difference on the working of my slash_color() function but I don't understand the origin.

It seems that a variable is missing in Debian version for awk, but I can't see which one.


Solution

  • Do not do: printf something. Always do printf "%s", something. The awk errors, because you passed invalid printf format specifiers %{ and %}, yet you did not pass any arguments. Do:

    printf "%s%s%s/", blue, $i, pink;
    

    I think you can just:

    {gsub("/", pink "/" blue)}1