For those that are regular Terminal, SSH, and/or Putty users, there will come a time when you want to change the Bash prompt.
Here is the new PS1 (Shell Prompt) which you can add into ~/.bashrc
if [ "$color_prompt" = yes ]; then<br /> PS1="\[$(tput setaf 4)\]\d [$(date +%H:%M)]\[$(tput setaf 2)\]\u@\h\[$(tput setaf 7)\]:\[$(tput setaf 5)\]\w\[$(tput setaf 7)\]$ \[$(tput sgr0)\]"<br />else<br /> PS1="${debian_chroot:+($debian_chroot)}\[\e[1;34m\][$(date +%H:%M)]\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[1;36m\]\w\[\e[00m\]\$ "<br />fi |
Note : The code above is assuming you are using Ubuntu, or a similar Linux distro. Using tput may not work with some consoles.
Explanation
PS1 is the environment variable used to determine the shell prompt. You can play around with and test the prompt by simply setting PS1 during standard use.
Default prompt
PS1=”\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ “
Output Example
Colour Prompt (tput)
PS1=”\[$(tput setaf 4)\]\d [$(date +%H:%M)]\[$(tput setaf 2)\]\u@\h\[$(tput setaf 7)\]:\[$(tput setaf 5)\]\w\[$(tput setaf 7)\]$ \[$(tput sgr0)\]”
Output Example
Colour Prompt (backup using \e)
PS1=”${debian_chroot:+($debian_chroot)}\[\e[1;34m\][$(date +%H:%M)]\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[1;36m\]\w\[\e[00m\]\$ “
Output Example
I find that the current time is useful to see when you were last doing things on the command line. The date can sometimes be useful for when you’ve haven’t touched the session for a few days but aren’t sure when you were in last, or for when saving the output as a backup in case things go wrong.
Unlike previous attempts these prompts don’t have line wrapping issues, which are a particular pain when running long commands and trying to edit them
Resources :
- http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ — A great, easy to read resource. I love the NixCraft cyberciti tutorials, they are always a joy to read.
- http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html — Has a great colour generator script at the end.
- http://www.hypexr.org/bash_tutorial.php — Has a lot of other useful information, especially regarding Bash completion.