Erik A. Hanson's Weblog

A Bold Prompt in Bash

Posted: February 12th, 2006    Tags: Howto

I wanted my Bash prompt to be bold and red so I could find my one line of input amongst the million lines of output in my terminal. I’m no Unix nerd, so I don’t really know offhand where to look for this info. After a bunch of Googling, I found something that works for me. The shell variable for the prompt is naturally called PS1. The original value was:

PS1='\\h:\\w \\u\\$ '

The code for starting a font and color change is \\e[. The code for bold red is 1;31. The code for closing the starting code for a font and color tag is m. The closing code for a font and color change is \\e[m. Bash thinks that these color commands will increase the length of the prompt, which will cause odd line wrapping behavior. The solution is to wrap all the color codes in brackets (\\[ and \\]).

So, the entire prompt looks like this:

PS1='\\[\\e[1;31m\\]\\h:\\w \\u\\$\\[\\e[m\\] ‘

Eeech. Add that to your .bashrc file and you’re all set. If you don’t want the red, the prompt would be:

PS1='\\[\\e[1m\\]\\h:\\w \\u\\$\\[\\e[m\\] ‘

More colors are listed at the bottom of this page.



Leave a Reply

About Me

I'm a software developer and consultant in San Francisco. I mostly develop web apps in Javascript, Java and Rails. There's a little more about me on my LinkedIn profile.

Recent Projects

wshlst.com: a group-oriented wish list website.

BoardsConsult​/​RotationTracker: a site to help medical residents prepare for the U.S. Medical Licensing Exam, and a related site to help medical schools evaluate and track the progress of the residents in their programs.

Jobs

I moderate a few completely free jobs mailing lists with RSS feeds and zero spam: XP jobs, Rails jobs and Mac development jobs. Also, my pals at Pivotal Labs have agile Rails and Java job openings.

Plug: Web Hosting

Looking for cheap shared web hosting and/or domain registration? I've been using DreamHost for years and I've been very happy with them. They offer tons of disk space and bandwidth, one-click installation for apps like WordPress and Gallery, unlimited domains, thousands of email accounts, private domain registration, and more. They are also a nice bunch of folks, not some big dumb company.