Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

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() {
    clear
    echo -e "Microsoft Windows 2000 [Version 5.00.2195]\n(c) Microsoft Corporation, 1985-2000.\n"
    function precmd() {
        PWD=$(pwd)
        PWD=${PWD/\/usr/\/Program Files}
        PWD=${PWD/\/home\/$(whoami)/\/Documents and Settings\\Administrator}
        PWD=${PWD/\/home/\/Documents and Settings}
        PWD=${PWD/\/sbin/\/Windows}
        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
add comment

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

e.g.

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
2  
@KevinBurke See Set variables in ZSH precmd and reference them in the prompt –  Gilles Feb 20 '12 at 2:04
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.