Search code examples
shellzsh

What ${ (%):-%n} means?


I don't know what "${ (%):-%n}" means.

${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh

The output of echo ${(%):-%n} is my username.

output:
output


Solution

  • The (%) is a parameter expansion flag, specifically the one that causes prompt escape sequences in the value of the parameter to be expanded. For example,

    % x="%n"
    % echo $x
    %n
    % echo ${(%)x}
    pi
    

    The :- operator with no parameter name causes the following text to be treated as the value of the expansion:

    % echo ${:-%n}
    %n
    

    Put them together, and you get an expression that expands to your current username. The string %n is the result of the parameter expansion, which also undergoes prompt expansion.

    % echo ${(%):-%n}
    pi