Add time to terminal

December 6th, 2013

!Disclaimer: I'm not an expert on the .bashrc file, so I can't guarantee nothing will break.

In linux you can make the system show the time as part of the terminal line like this:

[18:15] tss@localhost:~$

To achieve this on Ubuntu* you need to update the ~/.bashrc file.

First take a backup of the file - so you can revert something goes wrong.

$ cp ~/.bashrc ~/.bashrc.bak

Then update the file ~./.bashrc where it says the following (for me it was from line 60)

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\$
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

The else part is what is used (at least on my machine). To add the time in the Hour:Minutes format you need to add the \A flag where you want it. I put it in square brackets at the beginning like this:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\$
else
    PS1='[\A] ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

You don't need the square brackets and you can find other format codes (and other system information) by searching the web.

Categories

IT

Tags