Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to use the value of a variable set in my zsh shell to display in my prompt line.

For example, let's say export TOKEN='hello' is set in my session. At that time, I load my prompt which contains a "$TOKEN".

This works fine, and the prompt is correctly displayed. However, if I change the value of TOKEN, my prompt do not get updated.

How do I make zsh to update my prompt every time I have executed a command?

share|improve this question

migrated from Dec 12 '12 at 13:44

This question came from our site for professional and enthusiast programmers.

zsh runs on many other platforms aside from Linux, most importantly, Mac OS X. – hd1 Dec 11 '12 at 22:26
up vote 1 down vote accepted

There's a couple of ways you could do this. You should look into the precmd and preexec functions. They're in the hook functions section of zshmisc (which you can read online or using man zshmisc.

A simple solution would be to read this question, which says to put this in your ~/.zshrc file:

 setopt prompt_subst

This will do what you want:

 ┌─(simont@charmander:s000)─────────────────────────────────────▸▸▸▸▸▸▸▸▸▸─(~  )─┐
 └─(12:44)── export GREETING="foo"                                 ──(Wed,Dec12)─┘
 foo ┌─(simont@charmander:s000)─────────────────────────────────▸▸▸▸▸▸▸▸▸▸─(~  )─┐
 └─(12:44)── export GREETING="bar"                                 ──(Wed,Dec12)─┘
 bar ┌─(simont@charmander:s000)─────────────────────────────────▸▸▸▸▸▸▸▸▸▸─(~  )─┐

(This messes up my prompt a little, but you can see the foo and bar displaying nicely as they should, updating when $GREETING is changed. In your case, use $TOKEN).

My prompt heavily borrows from Phil!'s ZSH Prompt, which I found very useful when learning to customise mine.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.