This question already has an answer here:

I have a following bash prompt string:

root@LAB-VM-host:~# echo "$PS1"
root@LAB-VM-host:~# hostname 

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

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?

marked as duplicate by PersianGulf, cuonglm, X Tian, Gilles bash Jun 2 '15 at 23:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • No relation to that question. Read the question (and the answers) before you agree, people! – alexis Jun 2 '15 at 14:50
  • Why was this question marked as a duplicate? The questions look similar, but (a) the author's own answer in the other question is about making the hostname change permanent, and (b) there is no solution there for the problem that Martin asked about. For the second time: Read the question before you call it a duplicate! – alexis Jun 3 '15 at 9:53
  • @Gilles, is there a process for re-opening a question marked as duplicate? This one is superior to the other in many ways, IMHO. (The OP is not confused about the problem, and now there's actually an answer to what the question asks.) – alexis Jun 3 '15 at 9:55
  • @alexis You vote to reopen, but that requires 3000 reputation. I don't see what makes this question not a duplicate of the other. Maybe you could clarify that in an edit, or start a meta thread to discuss it. – Gilles Jun 3 '15 at 10:42
  • Can I try here first? In the other question, the user was confused about how changing the hostname works (the persistence of PS1 contributed to the confusion, but that's the extent of the similarity). Note that that user tried rebooting as a way to solve their problem, and eventually self-answered by figuring out how to set /etc/hostname properly, and rebooting. This question is about updating the prompt in a live bash session. And it has an answer (mine) that does that. – alexis Jun 3 '15 at 11:53

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:

export PS1="$PS1"

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 with exec bash)-- the hostname will be updated.

To automatically track hostname changes in all running shells, set your prompt like this in your .bashrc:

export PS1='\u@$(hostname):\w\$ '

or in your case:

export PS1='${debian_chroot:+($debian_chroot)}\u@$(hostname):\w\$ '

I.e., replace \h in your prompt with $(hostname), and make sure it's enclosed in single quotes. This will execute hostname before every prompt it prints, but so what. It's not going to bring the computer to its knees.

  • it would appear (having not looked at the bash source recently) that \h sets the hostname once, during parsing, while OP is really looking for the dynamic output from $(...) – Jeff Schaller Jun 2 '15 at 16:47
  • 1
    On OS X at least it's not during parsing PS1, but during bash start-up. That's why re-exporting PS1 doesn't change the value of \h. – alexis Jun 2 '15 at 16:50
  • @alexis No, Debian 7 with bash 4.2.37 does not pick up changed hostname if PS1 variable is re-exported. However, while I have changed the \h with literal hostname before, I really liked the exec and $(hostname)tricks. Thanks! – Martin Jun 3 '15 at 10:19

The hostname does not get updated automatically in the prompt. You have to reexport the $PS1 variable:

export PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$' 

This variable is already set in your .bashrc or other BASH config file, as shown by lines #1 and #2 of your output. But once you modify the hostname, you need to reexport the variable with the command above if you want the new prompt to get updated.

  • What system are you on? This doesn't work on OS X. – alexis Jun 2 '15 at 17:07
  • Red Hat, and as far as I know works on any other Linux. – dr01 Jun 2 '15 at 19:41
  • No, bash doesn't work that way (I just confirmed with 2.03 and 4.3). It only reads the hostname once when it starts, so subsequent hostname changes have no effect. – Gilles Jun 2 '15 at 23:13
  • @dr01 I can also confirm that this does not work(Debian 7 and bash 4.2.37). – Martin Jun 3 '15 at 7:39

According to dr01 said, You need to use :

export PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$' 

But above line is temporarily, If you want to use permnently, You have to use above line in your .bashrc or .profile

  • The $PS1prompt is already set in OP's config file, as we can see from the output. – dr01 Jun 2 '15 at 13:22
  • So you need one run it via source .profile or source .bashrc – PersianGulf Jun 2 '15 at 13:24
  • These commands reload the whole config file, something that the OP might want or not. – dr01 Jun 2 '15 at 13:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.