Skip to content

Instantly share code, notes, and snippets.

@fdemmer
Created August 6, 2012 07:56
Show Gist options
  • Save fdemmer/3272098 to your computer and use it in GitHub Desktop.
Save fdemmer/3272098 to your computer and use it in GitHub Desktop.
simple bash prompt customizer

Installation

Put prompt.sh in "/etc/profile.d" or any other place suitable for the target system to have it load when a user logs in.

In case the target system does not have a /etc/profile.d you can create it:

mkdir /etc/profile.d

It may be necessary to make it executeable:

chmod +x /etc/profile.d/prompt.sh

Then you may have to add the following to /etc/profile to load files in this directory:

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        . $i
    fi
done

Configuration is done via a "platform" file in "/etc/sysconfig" or "/etc/default".

# platform configuration
PLATFORM="LAB"
PLATFORM_COLOR=$LPURPLE
#PLATFORM="TEST"
#PLATFORM_COLOR=$YELLOW
NOTE=" "
# users to give ROOT_COLOR and ROOT_PROMPT
ROOT_USERS=("root" "oracle")
# user prompt
USER_PROMPT=">"
# root prompt
ROOT_COLOR=$LRED
ROOT_PROMPT="#"
#!/bin/bash
# define color codes
NOCOL="\[\033[0;0m\]"
BLACK="\[\033[0;30m\]"
DGRAY="\[\033[1;30m\]"
BLUE="\[\033[0;34m\]"
LBLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LGREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LCYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LRED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LPURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
LGRAY="\[\033[0;37m\]"
WHITE="\[\033[1;37m\]"
# set defaults:
PLATFORM=""
PLATFORM_COLOR=$NOCOL
ROOT_COLOR=$LRED
ROOT_PROMPT="#"
USER_PROMPT=">"
ROOT_USERS=("root" "oracle")
NOTE=" "
# try to load configuration
if [ -e /etc/sysconfig/platform ]; then
. /etc/sysconfig/platform
fi
if [ -e /etc/default/platform ]; then
. /etc/default/platform
fi
# determine username
USER=$(id|sed 's/^uid=[0-9]*(\(.*\)) gid.*$/\1/')
# change prompt for dangerous users
PROMPT=$USER_PROMPT
USER_COLOR=$NOCOL
for ROOT_USER in "${ROOT_USERS[@]}"; do
if [ $USER = $ROOT_USER ]; then
PROMPT=$ROOT_PROMPT
USER_COLOR=$ROOT_COLOR
fi
done
# various formats
#PS1="${USER}@${HOSTNAME} ${COLOR}${PLATFORM}${NOCOL} $PROMPT "
#PS1="\u@\h \t \w $PROMPT "
#PS1="${USER_COLOR}\u${NOCOL}@\h ${PLATFORM_COLOR}${PLATFORM}${NOCOL} \W $PROMPT "
#PS1="\u@\h ${COLOR}${PLATFORM}${NOCOL} \W $PROMPT "
#PS1="${PLATFORM_COLOR}${PLATFORM}${NOCOL} ${USER_COLOR}\u@\h${NOCOL} \W $PROMPT "
# default format
PS1="${PLATFORM_COLOR}${PLATFORM}${NOCOL}${NOTE}${USER_COLOR}${USER}@${HOSTNAME}${NOCOL} \W $PROMPT "
PS2="${USER} ? "
export PS1 PS2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment