0

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

2

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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