3

My current ubuntu prompt is set as something like

athos@myT460pLaptopBoughtAt2016 ~$ echo $PS1
[\u@\h \W]\$

, where h means Static hostname, as seen, it's quite long.

On the other side, I've set the Pretty hostname:

~$ hostnamectl
   Static hostname: myT460pLaptopBoughtAt2016
   Pretty hostname: T460p
...

Is there a way to refer to the Pretty hostname instead of the Static hostname in prompt?

athos
  • 155

1 Answers1

3

If you replace \h with the following custom function in your .bashrc (or where you define $PS1):

$(hostnamectl | awk '/Pretty hostname/ { printf $3 }')

That should do the trick.

You can test the output of the function by typing:

echo $(hostnamectl | awk '/Pretty hostname/ { printf $3 }')

On Ubuntu 22.04 and later, you can also use the --pretty option, which outputs the pretty hostname directly:

$(hostnamectl hostname --pretty)
Artur Meinild
  • 26,018