Wednesday, August 28, 2013

Colored bash in CentOS

The following is ripped from the Gentoo /etc/bash/bashrc with minor modifications for slight differences in CentOS:

use_color=false
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS.  Try to use the external file
# first to take advantage of user additions.  Use internal bash
# globbing instead of external grep binary.
safe_term=${TERM//[^[:alnum:]]/?}   # sanitize TERM
match_lhs="$(<${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
    if [[ ${EUID} == 0 ]] ; then
        PS1='\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
    else
        PS1='\[\033[01;32m\]\[email protected]\h\[\033[01;34m\] \w \$\[\033[00m\] '
    fi
else
    if [[ ${EUID} == 0 ]] ; then
        # show [email protected] when we don't have colors
        PS1='\[email protected]\h \W \$ '
    else
        PS1='\[email protected]\h \w \$ '
    fi
fi

unset use_color safe_term match_lhs
 
 
This script should be run after /etc/bashrc is run. Specifically, it depends on COLORS being set. This is done in /etc/profile.d/colorls.sh, which is sourced at the end of /etc/bashrc. One could put this in the users .bashrc after /etc/bashrc is sourced, or, for new users, in /etc/skel/.bashrc.

found at:  https://ashbyte.com/ashbyte/wiki/Centos/ColorizedPrompts.

No comments:

Post a Comment

Cybermap

Internet Storm Center Infocon Status

Internet Storm Center Infocon Status
Internet Storm Center Infocon Status