Tuesday, August 12, 2014

Very useful bash $PS1 setting

My ~/.bashrc settings have a killer setting for how my shell prompt looks:

case $- in
   *i*)
export PS1="\[$(tput bold)\]\[$(tput setaf 6)\]\t \[$(tput setaf 2)\][\[$(tput setaf 4)\]\u\[$(tput setaf 1)\]@\[$(tput setaf 4)\]\H \w \[$(tput setaf 6)\]\[$(tput setaf 2)\]]\[$(tput setaf 4)\] \$? \\$ \[$(tput sgr0)\]" ;;

     *)  return ;;
esac


The result:

22:19:43 [myuser@myserver.example.com ~ ] 0 $

This shows the current system time, username@server FQDN, pwd, exit code of last command, and regular user or root prompt.

This works in both interactive and non-interactive shells (hence the case statement).  Enjoy.  Additional info can be found here:

Bash $PS1 generator
10 Bash $PS1 examples
Bashrc generator

No comments: