4

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?

0

3 Answers 3

6

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 . )"
}
1
  • cd -P does the same.
    – anishsane
    Mar 9, 2022 at 7:14
0

To add a little help to those people like me who want PS1 to resolve with symlinks dereferenced, edit .bashrc thusly:

if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(readlink -f \w)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:$(readlink -f \w)\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: $(readlink -f .)\a\]$PS1"
    ;;
*)
    ;;
esac
0

Chances are you already have a customized PS1 variable in your ~/.bashrc file and you want to keep your other customizations.

Long story short... just go in ~/.bashrc, find where PS1 variable is defined, and replace \w with $( readlink -f . ) .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .