The Bourne Again Shell aka BASH has been around for a while. For those of us that also have been around for a while…this shell possibly could be the one you choose to use for your Linux distro. I first started using BASH when I was in college. We had Solaris 2.0 Servers that allowed me to mess around quite a bit back then.
But most new users fear the shell (or as windows likes to call it, command line) and venture there as little as possible. The prompt that greets most users that open a Linux shell is static and unyielding; yet, there are small adjustments that can make your Linux shell bend to the will of your force. Today, I’ll be going over some simple and fun ways to alter your .bashrc file, which is where your BASH “profile” is kept and read each time you login. Changes to this file can make your Linux shell a little bit friendly and less frightning.
Most shell’s look similar to this by default:
This doesn’t do much for you other than tell you who you are, what your hostname is, and what directory you are in. If you are like me, you want some useful information to be there so that what is above, becomes what is below:
Why is this so different? For starters, after seeing how to add color to your prompt you’ll be able to add your own color scheme to things. You’ll also be able to check out various ways to display information you want such as dates, times, whether you have mail, and your directory path you are in. Do you need l33t programming skills to accomplish this? Not at all! If you’re ready, let’s give this thing a try…
To begin with, there are bash shell flags and options that allow you to customize your prompt. These are not unlike flags and options you may use in your everyday life. For example, whenever I schedule an appt. for morning, I put A.M. and for afternoon, I put P.M. I’m sure most of you have done this before as well. In this case, A.M. and P.M. stand for something else and signify something to YOU, the user. So, when we begin to look at customizing bash prompts, there are some letters and symbols that we can use to accomplish our goal.
First off, let’s find WHERE we need to go. Open up a Konsole, Shell, Xterm, or whatever you may call your terminal in your Linux distro. We will be altering a file that contains your bash profile that is read each time you login. For most Red Hat based distros it will be named .bashrc (yours may be different, such as .bash_profile…if you can’t find it yet, just follow along) however, you won’t be able to see it unless special options are selected. For me, using the shell (konsole) I’m able to use the following command to list the files in the current directory:
While this is nice, we need to get to the right directory first…kind of like going into and out of folders in Windows. So, to do this, let’s get to your home directory. Using the command ‘cd’ which stands for ‘change directory’ we can navigate to the right/correct folder/directory we need to work in.
%> cd /home/username
Where username is YOUR username. In my case, it’s cd /home/devnet. So now, we’re in the correct directory where bashrc is located. Let’s list the files again.
Notice that you don’t see bashrc anywhere. This is because it is a system file. Just like Windows, you can’t always see system files. So, you need to add a ‘flag’ to your list command. This ‘flag’ operates just like a real flag does…a real flag would be flown outside your house or business and would mean something to someone and stand for something. In this case, our flag on our listing will also mean something…but it will mean something to us and the Linux operating system. Add the -a flag:
%> ls -a
Now you should see .bashrc (notice the ‘dot’ in front of it. All files with a dot in front can be found by adding the -a flag). If you’re using Kedit or Kate, you’ll have to change your view settings to see this file.
The bashrc file allows prompt string customization by using these backslash-escaped characters and color codes located beneath. Just like we used the -a flag to tell the list command what to do, you need to use the backslash to tell the bash prompt what to do:
\a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26") \e an ASCII escape character (033) \h the hostname up to the first `.' \H the hostname \n newline \r carriage return \s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24_hour HH:MM:SS format \T the current time in 12_hour HH:MM:SS format \@ the current time in 12_hour am/pm format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patchlevel (e.g., 2.00.0) \w the current working directory \W the basename of the current working directory \! the history number of this command \# the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number "nnn" \\ a backslash \[ begin a sequence of non_printing characters \] end a sequence of non_printing charactersCOLOR1="\[\033[0;36m\]" COLOR2="\[\033[0;32m\]" COLOR3="\[\033[0;36m\]" COLOR4="\[\033[1;37m\]"
For now, don’t worry about the above…especially the color stuff. That will give us easy ways to use color later on by allowing us to use COLOR4 instead of \033[1;37M\] everytime we needed to add some color to our prompt. Let’s continue on…just use the above as a reference. You’ll be able to use those \ designators to create a nice customized prompt. Ok, let’s get to cracking.
Open up your bash profile file that we spoke of earlier with your favorite text editor. For me, growing up on Solaris made me a pico and pine fan. Thus, I use the pico-like editor called nano for my editing.
%> nano .bashrc
Now…don’t worry about anything that is already here in bashrc. Drop down beneath it and let’s try our first prompt out. Let’s give it some flags and color to make it like mine earlier in the article. Cut and past the COLOR1 through COLOR4 stuff in the reference table above into .bashrc. Now drop beneath that and let’s put a line for your prompt. You need to signify that you’re adding a prompt with the letters PS and the numeral 1. So, add the following line to .bashrc:
Notice the quotation marks contain all your color codes and flags. Now let’s decipher what we just did. In the above referenced COLOR1 through COLOR4 we assigned actual colors that bash can recognize to the words COLOR1, COLOR2, etc. That way, we only have to type the words COLOR1 instead of those long, drawn out code-like commands and flags. Next, notice the \u flag. If you refer above, you will see that \u displays the username of the user. Ok, so what are these $ signs? In order for us to use the colors, we got to show bash the money. Therefore, to tell it COLOR1 actually has a different value, we put a $ sign in front of it. Let’s continue on. Next up, an @ symbol. This is by itself and has no \ flags or $ signs. So, it is what it is. An ‘at’ symbol. It will display as such. The same goes for the : in between $COLOR2 and $COLOR1. It’s a colon and it displays as such. Next up we have \\. Using the reference above, we see it is a backslash. So, we just displayed an extra backslash. Pretty simple eh?
Perhaps a different one using the same color scheme? Try this one:
PS1="$COLOR2($COLOR3\u@\h$COLOR2)-($COLOR1\@$COLOR2 $COLOR1\d$COLOR2)-($COLOR1\W$COLOR2)$COLOR1\n$COLOR1\\$ $COLOR4)"
This one should display similar colors, but would display like this:
(username@hostname)-(time & date)-(basename directory)
Notice the \@ flags for current time in 12 hour format (non military). We’ve already been over the colors so you know what those mean. The rest is just referring up to the reference list above.
Another cool thing to do is to include a quick code for when you’re logged in as root:
if [ "$UID" = "0" ]; then # I am root COLOR2="\[\033[0;31m\]" fi
That turns COLOR2 to bright red to signify that you are root. You can include this underneath all your prompt stuff you just did. For instance, the last prompt we created would look like this using the code above:
(username@hostname)-(time & date)-(basename directory)
Just cut and past it into your .bashrc and save. The changes will take place next time you logon.
So, experiment around. Try it out. You can make things as complex as you want them or as simple as you want them. There are also more colors:
Dark Gray 1;30
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Light Gray 0;37
To place these codes above into more colors (say COLOR5, COLOR6, etc.) simply take what we had above and integrate it. So, we had COLOR1 set like this:
Let’s put COLOR5 in as purple:
Easy as pie. Don’t forget to include your quotation marks and quote marks in your prompts. Generally, quotes and quotations must be closed (meaning that if you have one you’ll need another). The number one error I had when first starting was messing up my quotation marks :p
After you’ve settled on a prompt you like…you’ll need to export that prompt aka display it for everyone to see, including yourself. So, if you set your prompt like this:
PS1='\h:\w $' export PS1
The resulting prompt would look like this for me:
Notice the ‘export PS1’. Ensure you put this directly after your PS1= stuff and you’ll be set. Now let’s close and save .bashrc. Now the next time you login, you’ll see your new prompt. You can test this out by trying the command su. For instance, say I was logged in as root and changed devnet’s .bashrc. Now I’ll log in as devnet to check it out:
%> su devnet
Now I’m looking at my new improved prompt. If you aren’t root, you’ll be prompted for a password. But it’s better than logging out and logging back in. I hope this has been helpful to everyone.
If you have any questions with making your prompt or want to share some ultra cool wisdom on how you constructed your prompt with wicked awesome colors, please leave a comment on this article.
Thanks for reading and I hope this helps put some flavor on your bash prompt!
PS: Please post any errors you find with this article in the comments section and I’ll make the appropriate changes (with credit to those finding the errors )
This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.