Bash: How to Show a Colored Prompt

Submitted by admin on Mon, 03/14/2016 - 01:35

How to show a (colored) prompt like this?
[09/20 13:49][ubuntu@pixie:/etc]$

Here is how to show a prompt as follows:
[date_and_time][username@hostname:current_directory]$

Edit
vi .bash_profile

add add the following line:
PS1="[\$(date +%m/%d) \$(date +%H:%M)][\u@\h:\w]$ "

More colorful 1 (Pink)
export txt1='\[\033[38;05;218m\]' # Pink
export txt2='\[\033[38;05;177m\]' # Pink Dark
export txt3='\[\033[38;05;174m\]' # Pink Light
export txt4='\[\033[01;37m\]' # Gray

export txtrst='\[\033[00m\]' # Reset

# Pink
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 2 (Cyan)
export txt1='\[\033[38;05;75m\]' # Cyan Dark
export txt2='\[\033[38;05;122m\]' # Green light
export txt3='\[\033[38;05;81m\]' # Cyan
export txt4='\[\033[01;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Cyan
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 3 (Purple)
export txt1='\[\033[38;05;105m\]' # Purple Dark
export txt2='\[\033[38;05;75m\]' # Cyan Dark
export txt3='\[\033[38;05;98m\]' # Purple Dark
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Purple
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 4 (Orange)
export txt1='\[\033[38;05;166m\]' # Orange
export txt2='\[\033[38;05;222m\]' # Yellow Light
export txt3='\[\033[38;05;214m\]' # Orange
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Orange
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 5 (Grass)
export txt1='\[\033[38;05;105m\]' # Purple Dark
export txt2='\[\033[38;05;115m\]' # Cyan Green
export txt3='\[\033[38;05;151m\]' # Light Green
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Grass
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 6 (Rose)
export txt1='\[\033[38;05;202m\]' # Red
export txt2='\[\033[38;05;211m\]' # Pink
export txt3='\[\033[38;05;204m\]' # Shocking Pink
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Rose
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 7 (Ecology)
export txt1='\[\033[38;05;033m\]' # Blue
export txt2='\[\033[38;05;034m\]' # Green
export txt3='\[\033[38;05;042m\]' # Light Green

export txtwht='\[\033[00;38m\]' # White
export txtrst='\[\033[00m\]' # Reset

# Ecology
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txtwht}\w${txt1}]${txtwht}\$ ${txtrst}"

More colorful 8 (Sky)
export txt1='\[\033[38;05;038m\]' # Dark Blue
export txt2='\[\033[38;05;084m\]' # Light Green
export txt3='\[\033[38;05;117m\]' # Light Blue
export txt4='\[\033[01;37m\]' # Gray

export txtrst='\[\033[00m\]' # Reset

# Sky
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

More colorful 9 (Yellow)
export txt1='\[\033[38;05;223m\]' # Yellow Light
export txt2='\[\033[38;05;220m\]' # Yellow Dark
export txt3='\[\033[38;05;227m\]' # Yellow
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Yellow
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

And finally, type:
. .bash_profile

Bonus: How to add it to .bashrc remotely
sudo ssh username@example.com "cat << HERE >> ~/.bashrc
export txt1='\[\033[38;05;218m\]'
export txt2='\[\033[38;05;177m\]'
export txt3='\[\033[38;05;174m\]'
export txt4='\[\033[01;37m\]'
export txtrst='\[\033[00m\]' # Reset

# Pink
PS1=\"\\\${txt1}[\\\${txt2}\\\\\\\$(date +%m/%d) \\\\\\\$(date +%H:%M)\\\${txt1}][\\\${txt3}\\\u\\\${txt1}@\\\${txt3}\\\h\\\${txt1}:\\\${txt4}\\\w\\\${txt1}]\\\${txt4}\\\\\\$ \\\${txtrst}\"
HERE"

Tags