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 like echo $?
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
to uname -n
), if you cant find it feel free to send a message or just ask, and I will send you the code.
Im glad I was able to help!
Something that should be noted when adding colors to your shell prompt function is adding the non printable characters that keep the terminal from buggin out, this caused me a massive headache until I figured it out. When putting it in the PS1 variable directly you will put
\[
to begin a color sequence and\]
to end one, but printf will print a literal[
and]
so instead you will have to use\001
to start and\002
to end, I also recommend changing\e
to\033
or\x1B
to make things a bit more portable. For a quick example\[\e[1;31m\]
would become\001\033[1;31m\002
. Without these characters the terminal will like glitch out when you type a long command and then go back to the front.If you are like messing around or trying to learn a new programming language, you can try like porting your shell prompt to that language, Ive ported mine to C and set it using the same subsitution shell method, I thought it was a fun lil challenge.
But anyways, I hope you have fun customizing your shell prompt!