Much ado about scripting, Linux & Eclipse: card subject to change

2008-09-11

HOWTO: timestamp your Bash prompt

This has always been a pain for me -- waiting for crontab events to kick, you have to keep typing date or date;ls -la|tail waiting for files to show up.

Recently, I got annoyed enough to take matters into my own hands, and I found this. Most of the advice in there doesn't seem to work for the system I was working on, but I worked out another solution, using the PROMPT_COMMAND variable. The following code should go in your ~/.bashrc, /etc/bashrc, or equivalent.

red="\033[1;31m";
norm="\033[0;39m";
cyan="\033[1;36m";
if [ "$PS1" ]; then
    if [[ $UID -eq 0 ]]; then
      PS1="\[$red\]\u@\h:\w\\[\033[0;39m\]\n# "
    else
      PS1="\[$cyan\]\u@\h:\w\\[\033[0;39m\]\n\$ "
    fi
    export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]"
    export PS1=" "$PS1"\[\e]30;\u@\h\a\]"
fi
The result?
[15:17:16] nickb@emft:/opt
$ 
- or, when logged in as root -
[15:18:06] root@emft:/home/nickb
# 

3 comments:

Mr. Anderson said...

thanks a lot!

Daniel Reinhardt said...

Nice, but its not displaying the date on it.

buyi wen said...

you can try this free online timestamp converter to convert timestamp to readable date.