
Spice up your Bash!
Special thanks to Boondox for this article!
I like my console. I like my console so much I got tired at looking at the default bash prompt that is supplied with most distros out of the box. Its a boring grey prompt that gives you the basics. Thats fine and it works. If you use the console enough then you might want to spruce it up a bit. Here is how.
1) Backup your current prompt.
At your prompt type "echo $PS1", this will return your current prompt. I would write it down just in case. Doing this on my server looks like this "\u@\h:\w\$" which desplays "user@host:~$".
This doesnt look to bad, however the "~" character is your path and if your deep in a directory, your prompt could grow quite long as this one "user@host:~/downloads/podcasts/linuxbasement/season2$". this leaves little room on the current line. So lets try some new ideas and see where we are.
2) Try some new prompts without commitment!
At the colsole type PS1='\w\$'. The prompt now lookslike this "~/downloads/podcasts/linuxbasement/season2$". This makes the prompt a little smaller, but now you lost your username and hostname. If you have many users or servers, you may like to have that information. Lets try something else and add the current time. Type PS1='\t \w\$' and now we get "10:36:05 ~/downloads/podcasts/linuxbasement/season2$". The \? sequence is a code that the shell looks at and fills in data in place of that. Below are some other popular ones to use.
\d : The date ("Tue March 18")
\h : The hostname up to the first.
\H : The hostname. (hostname.domain.com)
\n : A newline
\t : Current time in 24-hour (HH:MM:SS)
\T : Current time in 12-hour (HH:MM:SS)
\@ : Current time in 12-hour am/pm
\A : Current time in 12-hour (HH:MM)
\u : The username
\w : Current working directory full path (home is ~)
\W : Current working directory (home is ~)
\$ : If root then # else $ (used for prompt terminator)
So lets build my current prompt. PS1='- \u@\h - \T\n- \w \$ ' This is how the prompt looks:
- user@host - 10:46:04 -
- ~/downloads/podcasts/linuxbasement/season2 $
Looks pretty neat to me. It displayes a bunch of information to me. Shows me the server name and username logged in. Also shows me the time, although not constantly updating, and then a new line shows the working folder I am in. This gives me plenty of room on my prompt.
3) Save it for later use.
This is a little tricky to explain for all distros out there. Here is how I did in under Ubuntu. Usually there is a file in your home directory named .bashrc and this file is where you would put your new prompt to load every time you go to the console. So edit your .bashrc file. I use nano so I will type nano .bashrc to open the file for editing. I usually do a Ctrl-W and type PS1 to see if this line is already defined. If it is then I comment out the current line and add my new prompt right below it. You can comment out the current prompt by adding the pound sign # in front of the line. If the PS1 does not exist then you could add it to the top or bottom of the file. The top is usually more accessable and easier to get to.
#PS1='\u@\h:\w\$'
PS1='- \u@\h - \T\n- \w \$ '
Once you save your file, log out and back in and you should have your new prompt.
For my next article I will do my best to explain color codes and making the prompt live and in color.
Hope it was simple enough!
Boondox
