Is there a way to set variables in the precmd
function of zsh and then echo them in the prompt?
Something like
function precmd {
GREETING='Hi Kevin!';
}
PROMPT="$GREETING";
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityPut this in your .zshrc
:
setopt prompt_subst
PROMPT='$GREETING'
Then $PROMPT
will undergo parameter expansion (as well as its siblings command substitution and arithmetic expansion) each time it's displayed. Take care to quote anything that gets included from an outside source properly. Also note that the result of the $
expansion will undergo %
expansion (i.e. prompt escape sequences can appear in $GREETING
).
An alternate approach is to reset the PROMPT
variable itself in precmd
, but this makes it harder to customize the prompt in a specific shell instance.