improving your bash prompt with a prompt command
You stare at your prompt all day every day, so we might as well make it look a little nicer, right?
I recently migrated to using a PROMPT_COMMAND, which allows you to get very creative with your prompt, and wanted to share it with y’all.
Above you can see how my prompt looks like.
In order, we have,
- a checkmark if last status was 0, x otherwise
- date/time when the prompt was printed (useful to time things if you forget to wrap them with time)
- hostname
- user
- current umask
- current path
- actual command prompt
The first line that includes status and date was my most recent addition, and so far I’ve been enjoying it.
How to set this up⌗
So let’s take a look at how to make your shell look like that.
Start by creating or opening your ~/.bashrc
.
Then create a method that will generate your prompt. I call mine __prompt:
__prompt() {
local EXIT="$?"
local RESET="\[\033[00;00m\]"
local HOST="\[\033[00;31m\]\h${RESET}"
local USER="\[\033[00;32m\]\u${RESET}"
local PATH="\[\033[00;33m\]\w${RESET}"
local UMASK="\[\033[00;34m\]$(umask)${RESET}"
local PROMPT="\[\033[01;00m\]$ ${RESET}"
local DT="\[\033[02;37m\]$(/bin/date "+%Y-%m-%d %H:%M:%S")${RESET}"
local STATUS=
[[ ${EXIT} != 0 ]] &&
STATUS="\[\033[00;31m\]x${RESET}" ||
STATUS="\[\033[00;32m\]✓${RESET}"
PS1="${STATUS} ${DT}\n${HOST} ${USER} ${UMASK} ${PATH}\n${PROMPT}"
}
and finally assign your custom __prompt
command to PROMPT_COMMAND:
PROMPT_COMMAND=__prompt
That’s it!
Source your .bashrc
or restart your terminal to test it out: . ~/.bashrc
Enjoy