BASH supports a $PROMPT_COMMAND
environment variable that defines a command to be executed before any first-level interactive prompt. I'm looking for a ZSH equilvalent of that.
The documentation says that there's a function precmd
I can define to achive that; however, I have no idea how to define it from an environment variable.
I've considered passing an environment variable that would make ZSH read a file containing the definition of that function, but ZSH doesn't seem to support such things: it only reads global files and then per-user files. I can replace them but I cannot add to them without modifying the files, which I cannot do.
So how do I define a pre-prompt hook in ZSH via an environment variable, like I'd do using $PROMPT_COMMAND
in BASH?
Best Answer
The simplest approach to emulate bash's
$PROMPT_COMMAND
which comes to my mind is to use theprecmd
hook, as you already figured out. Define it asand you can do something like that:
Please note the single quotes in that example, otherwise
$(date)
will get expanded too early, i.e. already when defining$PROMPT_COMMAND
and not when called before the prompt.If you want to preserve (and don't want to alter) the existing definition, you can use that approach:
With that the
prmptcmd
functions is executed after the existingprecmd()
function.Finally, here is a way which is suitable for use in a program package, which neither should modify user or system files nor can enter the commands interactive.
An example to spawn a bash session could be
To spawn zsh you can use
which causes
/program/dir/.zshrc
to be sourced. In this file theprecmd()
hook can be defined as explained above. If you want the user's settings in addition includesource $HOME/.zshrc
etc. in the program's .zshrc, too. This setup is maintainable, as no files outside the program directory are modified.As a last addition, here is a proof of concept how to keep the newuser welcome, too. Use the following code in your
/program/dir/.zshenv
rc config file: