Recently I stumbled over an article, about how to customize your shell prompt. What really surprised me, is that it lacked one of the most basic tips I learned nearly 20 years back: Always display a timestamp in the prompt, to be able to check how long a process is running or when it ended. (Don’t need it daily, but every so often it saves my butt. ;-)) The other trick is to always have a colorful prompt, to easily discern where output from programs start/stop. In total my PS1 looks like this (with GIT status at the end): [\e[32m]\u[\e[m]@[\e[35m]\h[\e[m] [\e[36m]\A[\e[m] [\e[37m][[\e[m][\e[31m]\w[\e[m][\e[37m]][\e[m]$(__git_ps1 “(%s)”)$
My question is, what customization, tips and tricks do you have for the shell prompt?
A person in this thread already recommended having different colors for different conditions like ssh and running as root, I havent seen anyone mention this specifically but you can determine if the current working directory is writable with something like
[ -w "$(pwd)" ]
and set the color to red or print a symbol if it doesnt return true.Also I recommend putting all the code and logic for your shell prompt in a shell function, and using a substitution shell to put it into the PS1 variable like this:
__shellprompt () { if [ "$(id -u)" = 0 ]; then local PROMPT_EMBLEM='#' else local PROMPT_EMBLEM='$' fi printf "%s" "$(whoami)@$(uname -n):$(pwd)" printf "\n%c " "$PROMPT_EMBLEM" } PS1='$(__shellprompt)'
Now this is just a really barebones example, there is a whole lot more you can do like passing in the last exit code through the argv of your shellprompt function like this
PS1='$(__shellprompt $?)'
and like print it out if its non-zero so you wont have to likeecho $?
to see if the last command failed, but you should be able to still do this. In my testing, running the shell prompt function in the subsitiution shell didnt effect the $? variable.In my first comment on another thread about shell prompts, I posted my full shellprompt, it is slightly outdated (I just changed
hostname
touname -n
), if you cant find it feel free to send a message or just ask, and I will send you the code.I can recommend Starship.
Definitely! Much more user-friendly and expandable than configuring PS1 manually.
I got different colors for Kubernetes clusters. Like green for testing cluster, yellow for development and red for production. Always taking a Quick Look before I do something
This sounds awesome. Do you change the color based on the selected cluster? Do you set an environment variable?
Yes this works with powerlevel10k theme for oh my zsh.