Tuesday, 9 September 2014

How to Show Date and Time in the Terminal

In this tutorial we will talk about how to show date and time in the terminal. By default, the “prompt” or terminal displays the user name with which we are connected, the machine name and the path where it is located. As follows:

So we see here that the prompt is “<username> @ <machinename>: <path>”.

Note: When the user is in his home, the path is replaced by a tild (“~”).

In this tutorial we will see how to change the appearance of the prompt to display the time, date, or other characters.


We must first understand that the form display prompt is specific to each user defined in the environment variable “PS1″ initialized to start a session. We can check the value of the environment variable “PS1″ with the following command:

[root@localhost usr]# echo $PS1

Then we have the following result:

We can therefore understand that:

“\U” = user

“@”: The symbol at sign

“\H” = the name of the machine

“\W”: the current path

“$” Sign dollars

Knowing this, we can freely change the appearance of our prompt. This requires that the time and date can be placed with the characters “\ t” for the time (“time”) and (“date”) “\ d” for the date. We can change the value of “PS1″ to add these characters with the following command:

[root@localhost usr]# PS1=’\d\t\u@\h:\W$’

We will immediately after a prompt like this:

You can of course change the display order of elements at will.

Make the Change Permanent

Since we have only changed the environment variable, this change will only be temporary. Indeed, restart another session, the environment variable will be reset. For the change to be permanent for the user must add our line to change the PS1 variable in the initialization of bash file with the following command:

[root@localhost usr]# echo “PS1=’\d\t\u@\h:\W$'” >> ~/.bashrc

Then restart a session for verification. To make this change permanent same for all users, you must make the same change on the common bash initialization file for all users:

[root@localhost usr]# echo “PS1=’\d\t\u@\h:\W$'” >> /etc/bash.bashrc

Hope, this tutorial will informative for you. Enjoy!


No comments:

Post a Comment