Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

If I type this in my PROMPT in my .zshrc file:

PROMPT="`pwd` >"

I expect that it will print the current working directory. However, pwd always prints /Users/kevin. Why doesn't it show my current working directory?

(I know that there is a function called %~ that will set it for you. I'm curious why this doesn't work though, this is a simpler case of something more advanced that I'm trying to do).

share|improve this question

Because "pwd" gets evaluated as soon as the variable PROMPT is set. Try echo $PROMPT.


renan ~ % PROMPT="`pwd` >"

/home/renan >echo $PROMPT
    /home/renan >

As you see, the "pwd" was replaced by the directory in which I was when I ran the PROMPT= command.

share|improve this answer
How can I delay execution/re-execute the function each time my PROMPT is shown? – Kevin Burke Feb 19 '12 at 20:34

The problem is that the backticks are not substituted every magical time, but only once, so your prompt is set to whatever pwd you were in. In order to reexecute something every time you enter a command, use function precmd() which should reset your $PROMPT. For example, this is a very useful function which resets your prompt according to the current directory:

function windows() {
    echo -e "Microsoft Windows 2000 [Version 5.00.2195]\n(c) Microsoft Corporation, 1985-2000.\n"
    function precmd() {
        PWD=${PWD/\/usr/\/Program Files}
        PWD=${PWD/\/home\/$(whoami)/\/Documents and Settings\\Administrator}
        PWD=${PWD/\/home/\/Documents and Settings}
        export PS1="C:${PWD//\//\\}> "

...or just use %~ in your PS1, like this:

PS1=' %n@%m: %~%# '

where %n is user, %m is host, %~ is current directory and %# is a superuser indicator.

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.