Linux

Personalizing your bash prompt


Based on my last blog entry, I thought I would give you some tips on personalizing your bash prompt. I've been doing this for a long, long time and I find it makes using your terminal more simple and a bit more fun.

Let's create a yellow prompt that says URHERE:. To do this do this we will be using some different characters (and combinations therein) not used in a standard prompt. All of the below will be written within your ~/.bashrc file.

The command to get this new, unique prompt looks like:

PS1='\[\033[1;33m\]URHERE: \w \[\033[0m\] '

What does this mean? Let's tear it apart and find out. The first sequence, `\[033[1;33m\] does the following:

  • ' - begin the string
  • \[ - begin a string of non-printing characters
  • \ - escape character preserving the literal value of the next character(s)
  • 033 - ascii code for the escape key
  • [ - used to separate the sequences
  • 1;33m - defines the color yellow
  • \w - adds the full path to the current working directory
  • \] - ends the string of non-printing characters

The next section, URHERE:, prints our message URHERE followed by the : symbol and a space for clarity. The final section, \[033[0m\]', ends our prompt with the following:

  • \] - begin a string of non-printing characters
  • \ - escape character preserving the literal value of the next character(s)
  • 033 - ascii code for the escape key
  • [ - used to separate the sequences
  • 0m - ends the color definition
  • \] - ends the string of non-printing characters

And there you have it. Of course you can customize this even further by changing colors or (of course) printed messages. To change the colors you will use the following strings:

  • 1;37m White
  • 37m Light Gray
  • 1;30m Gray
  • 30m Black
  • 31m Red
  • 1;31m Light Red
  • 32m Green
  • 1;32m Light Green
  • 33m Brown
  • 1;33m Yellow
  • 34m Blue
  • 1;34m Light Blue
  • 35m Purple
  • 1;35m Pink
  • 36m Cyan
  • 1;36m Light Cyan

One area I find color useful is to define various machines and their purposes. When logged into more than one machine I like to be able to tell which machine I'm working with (before I run the one command, on the wrong machine, that could bring down the entire network.) Because of this, I will chose a color scheme such as:

  • Blue = mail server
  • Yellow = ftp server
  • Red = firewall
  • Green = Workstation

That way I always know which machine I'm about to send a command to.

Do you have your own personal bash prompt configurations? Let us know in the comments below.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

2 comments
tanguy.arzel
tanguy.arzel

I use bash command windows for my daily work and I would like to customise the shell prompt to solve a problem I am facing. The issue is that I tend to create a lot of folders and subfolders and as I go "deeper and deeper" in the hierarchy tree, the shell prompt increases in length up to a point where it exceeds the window width and it jumps to the next line. I find this a bit annoying and I would like to know if I can customise the prompt so that it displays the last 3 directories for any place nested in more than 3 directories. example: % cd dir1 dir1 % cd dir1.1 dir1/dir1.1 % cd dir1.1.1 dir1/dir1.1/dir1.1.1 % cd dir1.1.1.1 dir1.1/dir1.1.1/dir1.1.1.1 % I did use in the past tcsh command window and the command looked like: alias setbar 'set d = $cwd; set a = (`echo $cwd | tr "/" " "`); @ n = $#a; @ nn = 0; @ nnn = 0; if ( $n > 3 ) @ nn = $n - 1; if ( $n > 3 ) @ nnn = $n - 2; if ( $n > 3 ) set d = "${a[$nnn]}/${a[$nn]}/${a[$n]}"; set prompt = "${hostname}: $d % "; unset n nn nnn a d'

CarlK2
CarlK2

Thanks to Jack Wallen for prompt info! Ever since SuSE said "bash$" I realized Unixy OSs were their own world. Linuxmag started w a Newbies column then gave up on the mass of Windose haters, leaving Linux opaque. Jack's goofs. 1. Typo ERR: 2nd section "\[" (Not\]) starts. 2. A little colorful explaination, please? What IS the ColorCode?! Your Primaries Color Codes suffix m : R= 31m= 1F= 0001 1111 G= 32m= 20= 0010 0001 R= 34m= 22= 0010 0010 Huh? Sometimes I see iirrggbb 8-bit string. That gives 2^2= 4 levels of each primary and 4 intensities.