Creative bash prompt

here is my bash prompt: :slight_smile:

GREEN=โ€œ[$(tput setaf 2)]โ€
RESET=โ€œ[$(tput sgr0)]โ€
PS1="${GREEN} \u \W \D{%F %T}>${RESET} "

just user, directory, date and time, all in green :smiley:
what could I add to make it more appealing? :slight_smile:

1 Like

Tip:

    export PROMPT_COMMAND=bash_prompt_command

You can write use e.g. your own bash function as bash_prompt_command, so the possibilities are limitless! :smile:

For example, when you cd to a particular folder, the function can do something specialโ€ฆ

1 Like

It is almost unlimited, but I have cut down on the extras now :grin:
Hereโ€™s the setup for my usual 2 liner:

BLACK="\e[1;30m"
RED="\e[1;31m"
GREEN="\e[1;32m"
YELLOW="\e[1;33m"
BLUE="\e[34m"
PURPLE="\e[1;35m"
CYAN="\e[1;36m"
WHITE="\e[1;37m"
LTBLACK="\e[1;90m"
LTRED="\e[1;91m"
LTGREEN="\e[1;92m"
LTYELLOW="\e[93m"
LTBLUE="\e[1;94m"
LTPURPLE="\e[95m"
LTCYAN="\e[1;96m"
LTWHITE="\e[1;97m"
RESET="\e[0m"
export PS1="\[${LTRED}\]\t \[${LTPURPLE}\]WD= \[${YELLOW}\][\w]\n\[${GREEN}\]\u\[${YELLOW}\]@\[${LTBLUE}\]\h\[${RESET}\] \$ "

This lives in the top of my .bashrc-personal file, giving this (in colours as shown, of course)
prompt
If you get far enough into the directory tree, it is nice to know where you are, without having to look a different amount out from the left edge to see your prompt. And the colours make it easier to spot a prompt when scrolling through back far enough!

2 Likes

Here is mine with different greens and font widths.
[time host current directory] $ or # depending on user or root
capture-200507-025002

PS1="\[$(tput bold)\]\[\033[38;5;10m\][\[$(tput sgr0)\]\[\033[38;5;2m\]\t\[$(tput sgr0)\] \[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;10m\]\h\[$(tput sgr0)\] \[$(tput bold)\]\w\[$(tput sgr0)\]\[\033[38;5;10m\]]\\$\[$(tput sgr0)\] \[$(tput sgr0)\]"

But hereโ€™s a bit of cheating for you: http://ezprompt.net/
or even more advanced: http://bashrcgenerator.com/

2 Likes

I originally looked for those helper sites, but I found that the first one created prompts that didnโ€™t correctly report their size to the terminal - made resizing the terminal into a nightmare :grin:

I think Iโ€™ll try for a mini-screenie as an edit for my previous postโ€ฆ It didnโ€™t come over well

The second one is the most useful one. You can select bits and pieces and then click on them to chose style (color, font-weight). Thatโ€™s how I generated the one above. I just chose what seemed like the most useful information to have in there.

A sidenote: I first tried and generated my own in a script file, using the documentation for the syntax. However my terminal started to behave strange, lines in terminal would overlap occasionally, making the whole thing unintelligible. A slight error in the escape sequence makes the whole string corrupt, interfering with and corrupting your terminal output.

When I started having problems with the terminal, I was tempted to forgo the whole thing, but then this tool saved my need for terminal fanciness :slight_smile:

As I understand it the important thing is to escape color code sequences, so that the shell sees them as null-size strings and they donโ€™t mess up the length of the prompt. Other than that only characters with special meaning need to be escaped (e.g. \u, \j, etc.).

I escape colors directly in their definition

## colors
BLACK='\[\033[1;30m\]'
BLUE='\[\033[1;34m\]'
GREEN='\[\033[1;32m\]'
CYAN='\[\033[1;36m\]'
RED='\[\033[1;31m\]'
PURPLE='\[\033[1;35m\]'
YELLOW='\[\033[1;33m\]'
WHITE='\[\033[1;37m\]'
NONE='\[\033[0m\]'

Mine :blush:

for user: (/home/.bashrc)

PS1="โ”Œ\[\033[1;35m\]\u:\[\033[m\]\w \[\033[1;36m\][`date`]\[\033[m\]\nโ””โ”€\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\](\342\234\223)\"; else echo \"\[\033[01;31m\](\342\234\227)\"; fi)\[\033[m\]โ”€โ”€(\[\033[1;37m\]#\[\033[m\])โ”€โ–ถ "

userbash

for root: (etc/bash.bashrc)

PS1="โ”Œ\[\033[1;31m\]\u:\[\033[m\]\w \[\033[1;36m\][`date`]\[\033[m\]\nโ””โ”€\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\](\342\234\223)\"; else echo \"\[\033[01;31m\](\342\234\227)\"; fi)\[\033[m\]โ”€โ”€(\[\033[1;37m\]#\[\033[m\])โ”€โ–ถ "

rootbash

byee

2 Likes

Thanks. Iโ€™ve been looking for those characters for a while now, time to make some more changes :grin:
I knew them as Windows codes (!) and as Amiga codes, but never learned the right way(s) to access them on Linux bash.

I use it in every Linux distro :slight_smile:
I cannot stay with stock prompt,I have to try different font to fix the end arrow.

I have LOTS of fonts, but trying to get specific characters out of them seems a bit problematic. There must be a standard way of accessing them somewhere, but I havenโ€™t got to that search yet :grin:

Great link, thanks :smiley: