How to change colours in bash prompt [duplicate]

I have followed the instructions here and in the howto link that is provided there. However nowhere does it appear to say how I can change the colours of the different parts, because currently I set it to:

PS1='${debian_chroot:+($debian_chroot)}[\t] \u@\h:\w\$ '

But the text is still all white, how could I perhaps make the numbers for the time green, and my username and computer name light blue? I am running Ubuntu GNOME 15.04.


Solution 1:

First of all, open up your ~/.bashrc file and enable color prompts:

nano ~/.bashrc

uncomment then line #force_color_prompt=yes

Then, change the PS1 line directly under the line if [ "$color_prompt" = yes ]; then

to:

PS1='${debian_chroot:+($debian_chroot)}\[\033[00;32m\][\t] \[\033[01;34m\]\u@\h:\w\$ '  

As you can see, 01:34m is light blue and 00:32m is green. Light green would be 01:32m instead of 00:32m.

Press CTRL + o and then press ENTER to save the file. Press CTRL + x to exit nano.

Then, run the following command to apply the changes by "sourcing" your bashrc file:

. ~/.bashrc

The changes should now apply to every newly opened terminal under your user.

click here for more info

Solution 2:

Here is one that a friend of mine and I have been working on. It gives a gray dashed line when you are a normal user, then if you are running commands as a root user, it changes to red for both the dashed line and the text.

enter image description here

In your .bashrc file, add the following code to the bottom of the file:

if [ -f "$HOME/.bash_ps1" ]; then
    . "$HOME/.bash_ps1"
fi

EDIT: Also, add it to the bottom of the /root/.bashrc as well. This is for if you switch to the root user by issuing the command sudo su -. (The rest of the edit is continued below code)

Then copy and paste the rest of this code to a new file called /home/<username>/.bash_ps1

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "

reset_style='\[\033[00m\]'

# determine if root or not
a=$(id|awk -F\( '{print $1}')
if [ "$a" = "uid=0" ]
then
    # for root
    status_style=$reset_style'\[\033[1;31m\]' # bold red; use 0;37m for lighter color
    command_style=$reset_style'\[\033[1;31m\]' # bold red
else
    # for other users
    status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
    command_style=$reset_style'\[\033[1;29m\]' # bold black
fi
prompt_style=$reset_style

# Prompt variable:

PS1="$status_style"'$fill $(date +"%m/%d/%y ")\t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\033[00m"' DEBUG

function prompt_command {
# create a $fill of all screen width minus the time string and a space:
let fillsize=${COLUMNS}-18
fill=""
while [ "$fillsize" -gt "0" ]
do
    fill="-${fill}" # fill with underscores to work on
    let fillsize=${fillsize}-1
done

# If this is an xterm set the title to user@host:dir
case "$TERM" in
    xterm*|rxvt*)
    bname=$(basename "${PWD/$HOME/~}")
    echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
    ;;
    *)
    ;;
esac
}

PROMPT_COMMAND=prompt_command

EDIT (Part 2): Now create a link to the .bash_ps1 in your /root folder

sudo -s
cd /root
ln -s /home/<username>/.bash_ps1

You can change any of the above code to fit your needs. This is one that I use actually at work so that I know if I am typing in commands as a root user that could be potentially dangerous. Plus, the time stamp appears just above each line you type, making it a little easier if you scroll back to see when you typed in that command.

Hopefully this helps!