Thursday, August 30, 2012

Configure bash prompt

I like a simple prompt that shows only the working directory rather than the full path. I don't do a lot of remote work, so I don't need the user and host information.  I color my prompt purple because ls uses that color only for symbolic links (magenta) so my prompt is not similar to typical ls ouput.  This differentiates the prompt from a wall of green (commands and files) and blue (directories) text.  Configure the bash prompt in the /home/USER/.bashrc file.  First, uncomment the force_color_prompt option as shown below:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
   The comments are part of the /home/USER/.bashrc file and the variable is commented out by default.  Next change the PS1 variable for both color and non color options from the default USER@HOST:FULLPATH$ to a simple CURRENTDIRECTORY$.  Comment out the default prompt and add the customized prompt below it.  That way you can revert if you make a mistake.
if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\W\$\[\033[0;0m\] '
else
#  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
   PS1='${debian_chroot:+($debian_chroot)}\[\033[01;0m\]\W\$ '
fi
unset color_prompt force_color_prompt
   To shorten the prompt change '\u@\h:\w\$ ' to '\W$'. The escape character '\033' (or \e) opens the color code, multiple color codes are separated with ';',  and the color code command is closed with 'm'.  I set the color prompt purple by changing the "\033[01;32m" (in the commented out line) to "\033[01;35m".  IBM has a good summary of color prompts, with a color chart, although they don't give the code, so I posted an example below.  The linux documentation project has thorough documentation of bash configuration.
   Here is a simple script to show the basic 8 color and 8 background combinations available to most shells.   The result of this script is shown in the subsequent screenshot.
#!/bin/bash
#show combinations of 8 colors and 8 backgrounds available to most shells

echo -ne "    "
for c in {0..7} 
do 
    echo -ne "  \033[1;37m 4${c}  \033[0m"
done

echo -e "\n"
for b in {0..7} 
do 
    echo -ne "\033[1;37m  3${b} \033[0m"
    for c in {0..7} 
    do 
    echo -ne "\033[4${c}m \033[3${b}m norm " 
    done
    echo -ne "\033[0m\n\033[1;37m  3${b} "
   for c in {0..7}
    do
    echo -ne "\033[4${c}m \033[1;3${b}m bold " 
    done
    echo -e "\033[0m\n"
done
   I change the /root/.bashrc file to have a prompt with ROOT in red, folder in yellow, and red text.  It is obnoxious, and is meant make a root shell obviously different so I don't do anything dumb.  I do not change my /root/.emacs file so a root emacs frame is obvious, for the same reasons.  The screenshot shows a user emacs frame on the left, then on the right (descending) a root emacs frame, a root xterm, and a user xterm.
You can get really crazy with 256 colors and other features.  Run this script to test if your shell supoprts 256 colors, and see if you like the results.
#!/bin/bash
#show 256 colors
for c in {0..255} ; do
    echo -e "\e[38;05;${c}m ${c} bash colors" 
done

1 comment:

  1. Thanks a lot for providing individuals with a spectacular possibility, I am always searching online for articles that can help me.
    used measurement equipment

    ReplyDelete