I have a following bash prompt string:
root@LAB-VM-host:~# echo "$PS1"
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
root@LAB-VM-host:~# hostname
LAB-VM-host
root@LAB-VM-host:~#
Now if I change the hostname from LAB-VM-host
to VM-host
with hostname
command, the prompt string for this bash session does not change:
root@LAB-VM-host:~# hostname VM-host
root@LAB-VM-host:~#
Is there a way to update hostname part of bash prompt string for current bash session or does it apply only for new bash sessions?
Best Answer
Does Debian really pick up a changed hostname if
PS1
is re-exported, as the other answers suggest? If so, you can just refresh it like this:Don't know about debian, but on OS X Mountain Lion this will not have any effect. Neither will the explicit version suggested in other answers (which is exactly equivalent to the above).
Even if this works, the prompt must be reset separately in every running shell. In which case, why not just manually set it to the new hostname? Or just launch a new shell (as a subshell with
bash
, or replace the running process withexec bash
)-- the hostname will be updated.To automatically track hostname changes in all running shells, set your prompt like this in your
.bashrc
:or in your case:
I.e., replace
\h
in your prompt with$(hostname)
, and make sure it's enclosed in single quotes. This will executehostname
before every prompt it prints, but so what. It's not going to bring the computer to its knees.