Filed in Product & Development by Trey Hoehne | June 28, 2012 4:30 pm
My name is Trey Hoehne, and I am a four-year Racker. I am also a Red Hat certified systems administrator, and am currently a Cloud Consultant and Evangelist. I travel a great deal, and work with our customers to help them use the Rackspace Cloud. I love meeting and hearing from our customers, and am also passionate about Linux and server management.
This Tech Corner explores the productivity improvements for server management that can be found by organizing your Bash prompts.
I find that it’s very easy to have several terminal windows open when working on multiple servers. However, I’ll be the first to admit that I’ve executed a command on the wrong server because of this. The chance of running a command in the wrong window is something that can be reduced by customizing Bash prompts for easy labeling.
For Bash, this is dictated by what variables are assigned to PS1. Take a quick look at this current Bash prompt and we’ll dissect it. To do this, we’ll need to echo $PS1 (which stands for primary prompt string) to see what variables are assigned:
[root@testtaketwo etc]# echo $SP1 [\u@\h \W]\$
It looks a little confusing but it’s actually pretty straightforward:
Here it is spaced out to match its output in the prompt:
[root@testtaketwo etc]# [\u @\h \W ]\$
So let’s add a little more functionality by editing the .bashrc file found in your user’s home directory. We’ll add the time (in 12-hour format) and date, as well as clean up the formatting a bit.
Add this to the bottom of ~/.bashrc in your favorite txt editor and save it:
PS1="[\u@\h:\W, \@, \d]\$"source .bashrc
And voila! We’ve added the time and date to our prompt and adjusted the formatting:
[trey@testtaketwo:~, 06:57 PM, Wed May 09]$
This can be taken a bit further by adding some color-coding to the prompts. This will let us quickly differentiate between boxes in our environment. For this example we’ll add a Prod tag before the hostname and make it red:
PS1="[\u@\e[1;31mPROD:\h\e[m:\W, \@, \d]\$ "
The addition of the color makes this look a lot more complicated than it really is, but if you break it down:
This starts the color scheme:
This ends it:
So the red color from the example is defined as:
With whatever you colored in between, notice the 1 in 1:31m, you can use that to toggle the brightness of the color so 0:31m will give you a darker red.
With prompt organization you can go on to create separate Bash prompts for your production and dev environments, making them easily distinguishable by color when you’re navigating between different terminal windows.
So this is great, but every time we add a new user we have to go in and edit their .bashrc file that lives in their home directory. Well, never fear! We can make this auto-magically go into effect for new users too. The /etc/skel contains files and directories that are copied over to a new user’s home directory when created via useradd, so if we edit the .bashrc file located here any new users will be created with the custom Bash prompt.
Source URL: http://blog.rackspace.com/treys-tech-corner-bash-prompts-and-you/
Copyright ©2015 The Official Rackspace Blog unless otherwise noted.