My bash prompt shows my hostname. If I change my hostname with the
hostname command, in
$HOSTNAME, the prompt still uses the old name. Is it possible to update this without logging out?
If you want your bash prompt to update dynamically as the hostname (as reported by the
/bin/hostname command) is updated, you can do this:
[root@foo ~]# PS1='[\u@`/bin/hostname` \W]\$ ' [root@foo ~]# hostname foo [root@foo ~]# hostname bar [root@bar ~]# hostname baz [root@baz ~]#
It's wasteful (runs a subshell every time you hit return -- not horrible but inelegant,) so I would only use this where the situation requires it. I use it when working on a base VM template that is used to generate multiple target VM instances where metadata about the instance is indicated in the hostname. And even then the custom
PS1 is only set temporarily, as part of the initial login session while the role VM instance is being deployed.