Search:

Beautifying the bash Prompt

Tired of the bash shell prompt? Especially when pathnames are getting really long as in "/var/www/my-www-server.example.com/data/downloads/debian/i386/network"? Ok, there is help.

After finding the bash prompt how-to I added

if [ "$BASH" ]; then
  getshortpath()
  {
        pwd | awk '// {
                n = split($0, x, "/");
                print (n <= 3)? $0: x[n-1] "/" x[n];
                exit (0);
                }'
  }

  PS1="\u@\h:\$(getshortpath) > "
fi

to my /etc/profile (of course, if you are not root you can put this in your ~/.bashrc or ~/.profile). This truncates the current directory path to it's last two directories which I find more important than the rest when it comes to shortening the path.

After you have done this the changes become active when you re-read /etc/profile with e.g.

# . /etc/profile

or after the next reboot of course. Do not forget to check if you own ~/.profile or ~/.bashrc does not overwrite the global definition. check this especially for root.

Applying some color

Ok, the PS1 above is not exactly my setup. Did you ever reboot a server, router or firewall by accident? Why does the workstation respond with "disconnected" after the reboot command - it never did this before?

Changing the bash prompt to

  PS1='\u@\[\e[33m\e[01m\]\h\[\e[0m\]:$(getshortpath) > '
  if /usr/bin/tty | awk '/./ { if ($1 ~ /^\/dev\/tty[0-9]$/) exit (0); else exit (1); }'; t
    PS1='\u@\[\e[32m\e[01m\]\h\[\e[0m\]:$(getshortpath) > '
  fi

it's coloured differently on a local console than on a remote session. This helps me most often to reboot the correct - local - machine and not something else. One thing to note here: X sessions on the local machine appear to be remote.

< dag | at | awk-scripting.de >