Colorful Bash Prompt

From WebOS Internals
Jump to: navigation, search

Edit /etc/profile and change:

if [ "$PS1" ]; then
# works for bash and ash (no other shells known to be in use here)
   PS1='\u@\h:\w\$ '
fi

to this:

if [ -n "${BASH_VERSION}" ] ; then
        if [ -f /etc/bash/bashrc ] ; then
                . /etc/bash/bashrc
        else
                PS1='\u@\h \w \$ '
        fi
else
        PS1="${USER:-$(type whoami >/dev/null && whoami)}@$(type uname >/dev/null && uname -n) \$ "
fi

Create this directory:

mkdir /etc/bash

Save the file below in /etc/bash/bashrc

# /etc/bash/bashrc                                                                                                        
if [[ $- != *i* ]] ; then                                            
        # Shell is non-interactive.  Be done now!                    
        return                                                       
fi                                                                   

shopt -s checkwinsize                                               

shopt -s histappend                                        

case ${TERM} in                          
        xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
                PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
                ;;                                                                           
        screen)                                                                              
                PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'
                ;;                                                                           
esac                                                                                         

use_color=true

safe_term=${TERM//[^[:alnum:]]/?}   # sanitize TERM              
match_lhs=""                                                     
[[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs}    ]] \
        && type -P dircolors >/dev/null \
        && match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true

if ${use_color} ; then
        # Enable colors for ls, etc.  Prefer ~/.dir_colors #64489
        if type -P dircolors >/dev/null ; then
                if [[ -f ~/.dir_colors ]] ; then
                        eval $(dircolors -b ~/.dir_colors)
                elif [[ -f /etc/DIR_COLORS ]] ; then
                        eval $(dircolors -b /etc/DIR_COLORS)
                fi
        fi

        if [[ ${EUID} == 0 ]] ; then
                PS1='\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
        else
                PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
        fi

else
        if [[ ${EUID} == 0 ]] ; then
                # show root@ when we don't have colors
                PS1='\u@\h \W \$ '
        else
                PS1='\u@\h \w \$ '
        fi
fi

unset use_color safe_term match_lhs

Please note that when running as root, ls --color will segfault. See How To Use "ls" In Color for more information.