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 currently have PS1='\W' but when in a symbolic link'd directory, the prompt shows the symlink name, how can I make PS1 to show the original directory name?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

On Linux, you can use the following:

export PS1='$( readlink -f . )'

Example:

$ export PS1='$( readlink -f . ) \$ '
/home/danielbeck $ ln -s /etc foo
/home/danielbeck $ cd foo
/etc $ _

Note that you still will be in /home/danielbeck/foo for everything else, like resolving parent directories with cd .., so, to continue the example:

/etc $ cd ..
/home/danielbeck $ _

Another option might be to replace cd with a function that enters the canonical directory instead of the symlink, something like:

function cd {
    if [[ $# -ne 1 ]] ; then
        builtin cd "$@"
    elif [[ "$1" = "-" ]] ; then
        builtin cd -
    else
        builtin cd "$( readlink -f "$1" )"
    fi
}

This might also work for any cd arguments and supports even CDPATH:

function cd {
    builtin cd "$@"
    builtin cd "$( readlink -f . )"
}
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.