Hack 34. Display username, hostname and basename of directory in the prompt

by Ramesh

The PS1 in this example displays following three information in the prompt:

  • \u – Username
  • \h – Hostname
  • \W – Base name of the current working directory
  1. -bash-3.2$ export PS1="\u@\h \W> "
  2.  
  3. ramesh@dev-db ~> cd /etc/mail
  4.  
  5. ramesh@dev-db mail>

In this example, the Linux prompt changed from the boring “-bash-3.2$” to a meaningful “ramesh@dev-db mail>”, in which it represents the following:

  • ramesh is the users who is currently logged into the Linux system.
  • dev-db is the host name of the Linux machine.
  • mail is the current working directory. i.e (/etc/mail)