1

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?

1
  • zsh runs on many other platforms aside from Linux, most importantly, Mac OS X.
    – hd1
    Dec 11, 2012 at 22:26

1 Answer 1

2

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 unix.stackexchange.com question, which says to put this in your ~/.zshrc file:

 setopt prompt_subst
 PROMPT='$TOKEN'

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)─────────────────────────────────▸▸▸▸▸▸▸▸▸▸─(~  )─┐
 └─(12:44)── 

(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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .