I may as well add to the fun here.
Some years back, probably the late '90s or so, there was a short lived
"Bash Prompt Project". I seem to recall it have a package in Debian
back around that time. I had previously done colorizing of the MS-DOS
prompt and had desired to do the same in Bash. Along the way I found
that project and I think made a contribution. It's dead now but my
colored prompts live on!
A couple of years ago I became fascinated with terminfo and so am using
it rather than specifying ANSI escape codes directly. Here is the
entirety of my ~/.bashrc that deals with setting the prompt:
---------------------8<-----------------------------------
# Git repository status in the prompt
if [[ -e /usr/lib/git-core/git-sh-prompt ]]; then
# From https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f
source /usr/lib/git-core/git-sh-prompt
export GIT_PS1_SHOWDIRTYSTATE=true # staged '+', unstaged '*'
export GIT_PS1_SHOWSTASHSTATE=true # '$' something is stashed
export GIT_PS1_SHOWUPSTREAM="auto" # '<' behind, '>' ahead, '<>' diverged, '=' no difference
export GIT_PS1_SHOWUNTRACKEDFILES=true # '%' untracked files
fi
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# 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
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
# Use variables set from terminfo capabilities to make PS1 and LESS_TERMCAP_*
# variables less cryptic.
#
# Colors: 0, Black; 1, Red; 2, Green; 3, Yellow; 4, Blue; 5, Magenta; 6, Cyan; 7, White.
# Foreground (text) colors
BLK=$(tput setaf 0)
RED=$(tput setaf 1)
GRN=$(tput setaf 2)
YEL=$(tput setaf 3)
BLU=$(tput setaf 4)
MAG=$(tput setaf 5)
CYA=$(tput setaf 6)
WHT=$(tput setaf 7)
# Background colors
BLKB=$(tput setab 0)
REDB=$(tput setab 1)
GRNB=$(tput setab 2)
YELB=$(tput setab 3)
BLUB=$(tput setab 4)
MAGB=$(tput setab 5)
CYAB=$(tput setab 6)
WHTB=$(tput setab 7)
# Character attibutes
BLD=$(tput bold)
ITA=$(tput sitm)
NOR=$(tput sgr 0)
# More prompt madness. Show exit status if command returned non-zero result.
prompt_last_exit_status () {
PROMPT_LAST_EXIT_STATUS=$?
if [[ ${PROMPT_LAST_EXIT_STATUS} == "0" ]]; then
PROMPT_LAST_EXIT_STATUS=
else
PROMPT_LAST_EXIT_STATUS='{'$BLD$RED$PROMPT_LAST_EXIT_STATUS$NOR'}'
fi
}
prompt_command () {
prompt_last_exit_status
# From: https://superuser.com/a/180275
CurDir=$(pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g")
}
export PROMPT_COMMAND=prompt_command
if [ "$color_prompt" = yes ]; then
PS1='('
PS1+='\[$CYA\]$SHLVL\[$NOR\]'
PS1+=')'
PS1+='['
PS1+='\[$YEL\]\j\[$NOR\]'
PS1+=']'
PS1+='\[$GRN\]\u\[$NOR\]'
PS1+='@'
PS1+='\[$BLD$GRN\]\h\[$NOR\]'
PS1+=':'
PS1+='\[$BLD$BLU\]$CurDir\[$NOR\]'
PS1+=' '
PS1+='$(__git_ps1 "(\[$MAG\]\[$ITA\]%s\[$NOR\])")'
PS1+=' '
PS1+='$PROMPT_LAST_EXIT_STATUS'
PS1+='\n'
PS1+='\[$BLD$WHT\]\$\[$NOR\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
--------------------->8-----------------------------------
Generally, for root accounts I change GRN to RED and on virtual machines
to MAG. Of course quite a range is available but the most limiting is
the Linux virtual console driver so I keep things relatively simple.
I also created some posts on my blog about tput[1] and how to reset the
color palettes of the various terminals[2].
- Nate
[1] https://www.n0nb.us/blog/2020/02/tinkering-in-the-terminal-with-tput/
[2] https://www.n0nb.us/blog/2020/02/coping-with-color-on-the-linux-console-and-xterm-and-friends/
--
"The optimist proclaims that we live in the best of all
possible worlds. The pessimist fears this is true."
Web: https://www.n0nb.us
Projects: https://github.com/N0NB
GPG fingerprint: 82D6 4F6B 0E67 CD41 F689 BBA6 FB2C 5130 D55A 8819
Attachment:
signature.asc
Description: PGP signature