Linux: screenFetch, neofetch and colored Bash prompt
Display system information with screenFetch or neofetch and colour the Bash prompt per environment.
This article shows how to display system information at login or on clear with screenFetch or neofetch, and how to colour the Bash prompt so that development, production and other environments are instantly recognisable.
Show system information
screenFetch
Install the script from the project repository (screenFetch) and make it executable:
sudo curl -o /usr/local/bin/screenfetch https://raw.githubusercontent.com/KittyKatt/screenFetch/master/screenfetch-dev && sudo chmod +x /usr/local/bin/screenfetch
To copy it to other servers, pack it into an archive, transfer it and unpack it with absolute paths:
sudo tar --absolute-names -cjvf screenfetchFN.bz2 /usr/local/bin/screenfetch
# list the archive content
tar --absolute-names -tvf screenfetchFN.bz2
# on the target
scp ~/screenfetchFN.bz2 target:\~
ssh -t target "sudo tar -Pxjvf screenfetchFN.bz2 && rm screenfetchFN.bz2"
Run screenFetch each time the screen is cleared:
echo -e '\nalias clear="clear && screenfetch"' >> ~/.bashrc
neofetch
Releases are published on the neofetch repository:
wget https://github.com/dylanaraps/neofetch/archive/refs/tags/7.1.0.zip
unzip 7.1.0.zip
cd neofetch-7.1.0/
sudo make install
neofetch
Do nothing in non-interactive shells
Anything added to ~/.bashrc (screenfetch, prompt) must only run in interactive shells, otherwise scp and other tools can break. Most distributions already include a guard at the top of the file:
Arch:
[[ $- != *i* ]] && return
Debian:
[ -z "$PS1" ] && return
Generic:
case $- in
*i*) ;;
*) return;;
esac
Coloured prompt
The prompt is defined by the PS1 variable. The escape sequence \e[<fg>;<bg>m sets the colours (foreground 30-37, background 40-47) and \e[m resets them. The whole escape sequence must be wrapped in \[ ... \] so that Bash computes the prompt length correctly.
| Code | Meaning |
|---|---|
\u |
user name |
\h |
host name |
\W |
current directory (base name) |
\w |
current directory (full path) |
One colour per environment
Development (white on green):
export PS1="\[\e[37;42m\]\u\[\e[m\]\[\e[37;42m\]@\[\e[m\]\[\e[37;42m\]\h\[\e[m\]\[\e[37;42m\] \[\e[m\]\[\e[37;42m\]\W\[\e[m\]\[\e[37;42m\]>\[\e[m\] "
Production (black on red):
export PS1="\[\e[30;41m\]\u\[\e[m\]\[\e[30;41m\]@\[\e[m\]\[\e[30;41m\]\h\[\e[m\]\[\e[30;41m\] \[\e[m\]\[\e[30;41m\]\W\[\e[m\]\[\e[30;41m\]>\[\e[m\] "
Acceptance or test (black on yellow):
export PS1="\[\e[30;43m\]\u\[\e[m\]\[\e[30;43m\]@\[\e[m\]\[\e[30;43m\]\h\[\e[m\]\[\e[30;43m\] \[\e[m\]\[\e[30;43m\]\W\[\e[m\]\[\e[30;43m\]>\[\e[m\] "
Make it permanent
Append the line to ~/.bashrc, for example for the development prompt:
echo "export PS1=\"\[\e[37;42m\]\u\[\e[m\]\[\e[37;42m\]@\[\e[m\]\[\e[37;42m\]\h\[\e[m\]\[\e[37;42m\] \[\e[m\]\[\e[37;42m\]\W\[\e[m\]\[\e[37;42m\]>\[\e[m\] \"" >> ~/.bashrc
Other examples
export PS1="\[\e[31m\]\u\[\e[m\]@\[\e[33;40m\]\h\[\e[m\] \[\e[34;43m\]\W\[\e[m\] > "
export PS1="\[\e[32m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[36m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\[\e[32;47m\]\\$\[\e[m\] "
Terminal colours (MobaXterm)
The same idea works in the terminal itself: use a dark green background for development and a dark red one for production.
| Environment | Background | Font |
|---|---|---|
| Development | #002200 (R 0, G 34, B 0) |
#ECECEC (R 236, G 236, B 236) |
| Production | #2B0000 (R 43, G 0, B 0) |
#ECECEC (R 236, G 236, B 236) |
Going further: powerline prompts
- Powerline and its patched fonts
- Trueline: Bash powerline-style prompt with true colour support