Bash Prompt

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 :

About admin

Australian Network for Art and Technology [ANAT] e: [email protected] | ph: +61 8 8231 9037 www.anat.org.au | www.filter.org.au | www.synapse.net.au Twitter: __ANAT | Facebook: http://bit.ly/bF9fXl The Australian Network for Art and Technology (ANAT) is supported by the Visual Arts and Craft Strategy, an initiative of the Australian, State and Territory Governments; the Australian Government through the Australia Council, its arts funding and advisory body, and the South Australian Government through Arts SA.
This entry was posted in Code. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *