Wednesday, February 15, 2012
5:06 PM

How To Modify Bash Prompt

Bash Shell is a powerful stuff in Linux. We can do almost everything there. Installing and removing applications, doing system upgrade, downloading files, even listening to your favorite songs. Although, there are many GUI applications which can do such tasks, many people still love using command prompt because it's handy, incredibly fast and we can see exactly happens in the background. If you are one of those people who loves using command prompt to perform such tasks, you might want to read this post on how to modify bash prompt.


What is bash prompt? Bash prompt is a like a bridge connects you to the shell. Bash prompt is a place where you type a command to ask the shell to perform a task. Bash prompt is usually represented by a dollar sign "$".

By default, Ubuntu and its derivative has bash prompt with the following format:
  • hok00age@Laptop-Q:~$
The above bash prompt format can be divided into three parts:
1. The word "hok00age" is username who types the command.
2. The word "Laptop-Q" is the hostname of the computer.
3. The character "~" is the working directory of current shell.

Today, I'd like to show you how to change this bash prompt format into something like this:
  • [08:47:50][hok00age]:~$
The first part of the above format "[08:47:50]" is the current time of the computer.

How to do that? Simply type the following command on currently opened Terminal:
  • export PS1="[\t][\u]:\w\$ "
Please pay attention on the above command. The part who will become "current time" in bash prompt is "\t", "\u" will become username, and you can guess that "\w" will become working directory. The key of this trick is bash prompt special characters to display any information on the shell. You can see all of available special characters that can be used to modify bash prompt here.

Please note, that any modifications you've made only affect the currently active shell. In other words, if you open a new Terminal emulator window, bash prompt is still in Ubuntu default. To make it permanent, you have to edit ".bashrc" file located in home directory. Open it with your favorite text editor or simply type the following command:
  • gedit ~/.bashrc
All you need to do is adding export PS1="[\t][\u]:\w\$ " line to the end of .bashrc file (see the figure below):


Now save the change and test it by opening new Terminal window.

Regards :)

0 comments:

Post a Comment