When does Zsh prompt expansion take place, especially in what order compared to other regular expansion
For example, in Bash 3.2.57, if you pass an escape for the prompt to a length function that looks like this, the length after the escape has been deployed will be returned:
bash-3.2$pwd
/tmp/prompt
bash-3.2$catset_prompt.bash
length()
{
echo "${#1}"
}
PS1 = '$(length\w)\w$'
bash-3.2$source./set_prompt.bash
11/tmp/prompt$
However, if you do the same with Zsh 5.7.1, you behave as if you were checking the length before deploying prompt expansion.
prompt%pwd
/tmp/prompt
prompt%cat./set_prompt.zsh
set-o PROMPT_SUBST
length()
{
echo "${#1}"
}
PS1 = '$(length "%d")%d$'
prompt%source./set_prompt.zsh
2/tmp/prompt$
In other words, I feel that expansion for prompts is deployed differently between Bash and Zsh.
When will the Zsh prompt expansion take place?It seems to be different from Bash as above, but how can I proceed with the prompt expansion before the command expansion like Bash?
shellscript zsh
The Zsh document says:
If the PROMPT_SUBST
option is set, the prompt string is first subject to parameter expansion, command substitution and arthmic expansion.
Therefore, after these three expansions, prompt expansion appears to occur.
If you want to do prompt expansion first, you can use print-P
.
prompt%pwd
/tmp/prompt
prompt%cat./set_prompt.zsh
set-o PROMPT_SUBST
length()
{
echo "${#1}"
}
PS1 = '$(length"$(print-P%d)")%d$'
prompt%source./set_prompt.zsh
11/tmp/prompt$
© 2024 OneMinuteCode. All rights reserved.