Colorful Bash Prompt

From WebOS Internals
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Edit /etc/profile and change: <source lang="bash"> if [ "$PS1" ]; then

  1. works for bash and ash (no other shells known to be in use here)
  PS1='\u@\h:\w\$ '

fi </source> to this: <source lang="bash"> 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 </source>

Create this directory: <source lang="bash"> mkdir /etc/bash </source>

Save the file below in /etc/bash/bashrc <source lang="bash">

  1. /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 </source>

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