Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com 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
add comment

1 Answer

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

share|improve this answer
add comment

Your Answer

 
discard

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.