How To Customise Linux Terminal Prompt
I’m using a very low powered laptop. When I fire up Docker containers, they can take a while. I’d like to know how long, but I didn’t check when I started it. If the terminal prompt had a timestamp then I wouldn’t need to.
It might be kinda useful info to have. Naturally, I’m going to spent multiple hours to finding and implementing the solution.
Here’s what I currently get:
<user>@<host>:<current directory>$
Here’s what I want:
[<YYYY-MM-DD-HH-MM-SS-TZ>]<user>@<host>:<full path>
$
How to change it in three simple steps:
- Modify the PS1 environment variable in the
.bashrc
file found in your home directory. Useman bash
(the PROMPTING section) or bashrcgenerator.com. .bashrc
already has a few lines involved in setting the PS1 var. Find out what$debian_chroot
is, and why you why want to leave it in the terminal prompt. chrooted debian?- Take the time to learn about
tput
and add colour commands. tput?, 256 colours
Here’s what was in there already:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
And here’s my first attempt at customisation:
BG_WHITE="$(tput setab 15)"
BG_BLACK="$(tput setab 0)"
FG_NAVY="$(tput setaf 4)"
FG_TEAL="$(tput setaf 6)"
FG_PURPLE="$(tput setaf 5)"
FG_RED="$(tput setaf 9)"
FG_BOLD="$(tput bold)"
DEFAULT_ATTRS="$(tput sgr0)"
if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+${BG_BLACK}${FG_WHITE}${FG_BOLD}($debian_chroot)}${DEFAULT_ATTRS}"
PS1+="${BG_WHITE}${FG_RED}${FG_BOLD}\[\D{%Y-%m-%d-%H:%M:%S}\]"
PS1+="${FG_PURPLE}\u@\h"
PS1+="${DEFAULT_ATTRS}${BG_SILVER}${FG_RED}:"
PS1+="${FG_TEAL}${FG_BOLD}\w\n"
PS1+="${FG_RED}\$ ${DEFAULT_ATTRS}"
else
PS1="${debian_chroot:+($debian_chroot)}"
PS1+="\[\D{%Y-%m-%d-%H:%M:%S}\]"
PS1+="\u@\h"
PS1+=":"
PS1+="\w\n"
PS1+="\$ "
fi
Caveat 1: The time in the prompt is the time the last command finished rather than the time when the current command started
Caveat 2: I don’t know where to find where colour preferences have been set, so I’m hardcoding them here. It might look odd if I changed my colour preferences