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?
3 Answers
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 . )"
}
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
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 . )
.