Colorer mon prompt, ça m'a toujours semblé stupide. Puis j'ai eu un Raspberry Pi avec Raspbian dont la conf' par défaut colore et met en gras le prompt et en sépare chaque partie (login@hostname, répertoire courant,...) avec une espace. Et j'en ai vu l'intérêt : la couleur et l'espacemment mettent en avant $ ou # ainsi que le répertoire courant. C'est vraiment agréable.
Du coup, j'ai décidé de déployer ça sur toutes mes autres machines. Par rapport au prompt RPi, j'ai ajouté :
* Dans "login@hostname", si login = utilisateur normal, alors afficher le login en couleur lavande. Si login = root, alors on le met en rouge. Pour ce faire : un .bashrc différent dans le $HOME de mon user et dans celui de root. ;)
* Affichage du code de retour de la dernière commande.
Pour que ça tombe en marche, il y a plein de subtilités :
* Si vous utilisez screen et les couleurs étendues (256 au lieu de 8), il faut activer le support 256 couleurs dans screenrc (voir
https://wiki.archlinux.org/index.php/GNU_Screen#Use_256_colors). Je fais ça system-wide en ajoutant ceci dans /etc/screenrc :
« attrcolor b ".I" # allow bold colors - necessary for some reason
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm' # tell screen how to set colors. AB = background, AF=foreground
defbce on # use current bg color for erased chars »
* Sur certaines installations, comme sur les KS d'OVH ou ash (le shell de busybox), le shell lit uniquement /etc/profile et .profile... Pour harmoniser, on peut créer un .profile qui lance le .bashrc :
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Note : ash ne supporte pas tput pour le formatage. ;) Il faut faire sans avec « \[\033[1;91m\] » par exemple (notons le « 1 » pour mettre en gras).
* Il faut aussi prendre en compte le fait qu'un terminal ne sera pas toujours associé à votre session SSH. C'est le cas avec rsync, par exemple. Donc tput foirera. Exemple d'output :
« tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified »
Pour corriger ça, il faut utiliser la construction logique « if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then [...] fi » que l'on trouve dans le squelette de bashrc sous Debian. Chez moi, ça donne donc :
« # set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# 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 [ -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
if [ "$color_prompt" = yes ]; then
PS1="\[$(tput bold)\]\[\033[38;5;141m\]\u\[\033[38;5;15m\]\[\033[1;90m\]@\[\033[38;5;15m\]\[\033[1;92m\]\h\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput bold)\] \[\033[1;94m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\] \[\033[1;33m\]\[$(tput bold)\]\$?\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput bold)\]\[\033[38;5;150m\]\\$\[\033[38;5;15m\]\[$(tput sgr0)\] "
else
PS1="\u@\h \w \$? \$ "
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac »