6

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Martin
  • 7,284
  • 40
  • 125
  • 208
  • **No relation** to that question. **Read the question** (and the answers) before you agree, people! – alexis Jun 02 '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 03 '15 at 09: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 03 '15 at 09: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](http://meta.unix.stackexchange.com/questions/ask?tags=discussion+specific-question) to discuss it. – Gilles 'SO- stop being evil' Jun 03 '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 03 '15 at 11:53
  • PS Right, vote to reopen. Thanks. I forgot I'm under 3000 on this site. – alexis Jun 03 '15 at 11:54
  • Let's put it a different way: Martin, the OP for this question, can get absolutely no help by reading the answers to the ["already answered"](http://unix.stackexchange.com/questions/15643/i-changed-my-hostname-why-is-my-bash-ps1-prompt-unchanged) question. – alexis Jun 03 '15 at 11:55

3 Answers3

8

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.

alexis
  • 5,719
  • 3
  • 21
  • 28
  • 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 02 '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 02 '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 03 '15 at 10:19
1

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.

dr_
  • 28,763
  • 21
  • 89
  • 133
1

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

PersianGulf
  • 10,728
  • 8
  • 51
  • 78