Customizing Bash Prompt

From SlackWiki
Revision as of 20:40, 15 May 2012 by Supyrow (talk | contribs)
Jump to navigation Jump to search


Originally posted by SU_PyRoW http://compelitepc.host56.com

There are allot of white papers on customizing the bash prompt. This one is of course different... by a small margin. It starts by mapping out colors from "\033" or "ESC" commands into text commands for better reading, modifying or if you change your background and have a transparent terminal running and need to change colors again with out having to remember or look up the color codes.

 
#.bashrc 
DULL=0
BRIGHT=1
FG_BLACK=30
FG_RED=31
FG_GREEN=32
FG_YELLOW=33
FG_BLUE=34
FG_VIOLET=35
FG_CYAN=36
FG_WHITE=37
FG_NULL=00
BG_BLACK=40
BG_RED=41
BG_GREEN=42
BG_YELLOW=43
BG_BLUE=44
BG_VIOLET=45
BG_CYAN=46
BG_WHITE=47
BG_NULL=00

ESC="\033"
NORMAL="\[$ESC[m\]"
RESET="\[$ESC[${DULL};${FG_WHITE};${BG_NULL}m\]"
BLACK="\[$ESC[${DULL};${FG_BLACK}m\]"
RED="\[$ESC[${DULL};${FG_RED}m\]"
GREEN="\[$ESC[${DULL};${FG_GREEN}m\]"
YELLOW="\[$ESC[${DULL};${FG_YELLOW}m\]"
BLUE="\[$ESC[${DULL};${FG_BLUE}m\]"
VIOLET="\[$ESC[${DULL};${FG_VIOLET}m\]"
CYAN="\[$ESC[${DULL};${FG_CYAN}m\]"
WHITE="\[$ESC[${DULL};${FG_WHITE}m\]"
BRIGHT_BLACK="\[$ESC[${BRIGHT};${FG_BLACK}m\]"
BRIGHT_RED="\[$ESC[${BRIGHT};${FG_RED}m\]"
BRIGHT_GREEN="\[$ESC[${BRIGHT};${FG_GREEN}m\]"
BRIGHT_YELLOW="\[$ESC[${BRIGHT};${FG_YELLOW}m\]"
BRIGHT_BLUE="\[$ESC[${BRIGHT};${FG_BLUE}m\]"
BRIGHT_VIOLET="\[$ESC[${BRIGHT};${FG_VIOLET}m\]"
BRIGHT_CYAN="\[$ESC[${BRIGHT};${FG_CYAN}m\]"
BRIGHT_WHITE="\[$ESC[${BRIGHT};${BG_WHITE}m\]"
REV_CYAN="\[$ESC[${DULL};${BG_WHITE};${BG_CYAN}m\]"
REV_RED="\[$ESC[${DULL};${FG_YELLOW}; ${BG_RED}m\]"

PROMPT_COMMAND='export ERR=$?'

If you happen to use Eterm or other advanced terminal its possible you need a ".bash_profile" here is one you can use if you don't have one.

#.bash_profile
source .bashrc
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi 
#End Script

Lets put it to work: I personally would comment out my currently working ##"PS1=" and put it below it in case you need to change it back for some reason.

PS1="${GREEN}YOUR COMPANY NAME HERE${BRIGHT_YELLOW}[${YELLOW}\u@compelitepc.host56.com${BRIGHT_YELLOW}]\n
${BRIGHT_CYAN}[${CYAN}\t${BRIGHT_CYAN}]${BRIGHT_VIOLET}[${VIOLET}\$(lsMB)${BRIGHT_VIOLET} |
${VIOLET}\$(lsMiB)${BRIGHT_VIOLET}]${BRIGHT_YELLOW}[${YELLOW}\w${BRIGHT_YELLOW}]\n
${BRIGHT_YELLOW}[${YELLOW}\!${BRIGHT_YELLOW}]${BRIGHT_CYAN} %> ${RESET}"

Should look like this minus the colors:

YOUR COMPANY NAME HERE[support@compelitepc.host56.com]
[13:57:23][590.478 MB|563.316 MiB][/media/cdrom]
[503] %> 

Note: the [503] is the history line number for those who cant remember what they typed or want to input something from the far past.

Now... To get the sum of files in current working directory, I made two files which do the summing. "lsMB" and "lsMiB" which on my system are both located in ~/bin as executable. Here is lsMB:

#!/bin/bash
#lsMB free for anyone who wishes to use and implement it, just please show your work. ;)
let TotalBytes=0
for Bytes in $(ls -l -a | grep "^-" | awk '{ print $5 }')
do
   let TotalBytes=$TotalBytes+$Bytes
done
if [ $TotalBytes -lt 1024 ]; then
   TotalSize=$(echo -e "scale=3 \n$TotalBytes \nquit" | bc)
   suffix="B"
else if [ $TotalBytes -lt 1048576 ]; then
   TotalSize=$(echo -e "scale=3 \n$TotalBytes/1024 \nquit" | bc)
   suffix="kB"
else if [ $TotalBytes -lt 1073741824 ]; then
   TotalSize=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc)
   suffix="MB"
else if [ $TotalBytes -lt 1099511627776 ]; then
   TotalSize=$(echo -e "scale=3 \n$TotalBytes/1073741824 \nquit" | bc)
   suffix="GB"
fi
fi
fi
fi
echo -n "$TotalSize $suffix"
#End Script

Finally, this is the lsMiB executable file located in "~/bin" This has the modification for the conversion to KiB, MiB & GiB one could further the code to handle TiB easily by doing some research on it... Try wiki!

#!/bin/bash
#lsMiB free for anyone who wishes to use and implement it, just please show your work. ;)
let TotalBytes=0
for Bytes in $(ls -l -a | grep "^-" | awk '{ print $5 }')
do
   let TotalBytes=$TotalBytes+$Bytes
done
if [ $TotalBytes -lt 1024 ]; then
   TotalSize2=$(echo -e "scale=3 \n$TotalBytes \nquit" | bc)
   suffix2="B"
else if [ $TotalBytes -lt 1048576 ]; then
   TotalSize2=$(echo -e "scale=3 \n$TotalBytes/1024*.976 \nquit" | bc)
   suffix2="KiB"
else if [ $TotalBytes -lt 1073741824 ]; then
   TotalSize2=$(echo -e "scale=3 \n$TotalBytes/1048576*.954 \nquit" | bc)
   suffix2="MiB"
else if [ $TotalBytes -lt 1099511627776 ]; then
   TotalSize2=$(echo -e "scale=3 \n$TotalBytes/1073741824*.931 \nquit" | bc)
   suffix2="GiB"
fi
fi
fi
fi
echo -n "$TotalSize2 $suffix2"
#End Script

One would be able to see the changes if 1) Restart the Terminal or 2) type:

$ source ~/.bashrc

and you will instantly see the changes.