7

Browsing the last command's results in Bash is a bit hard because the command prompt and results are the same color. This makes it hard to separate the results of consecutive commands.

How do I change the color to pink for example?

  • 1
    You'll want to add color to the PS1 variable. – Rob May 8 '12 at 17:34
  • Yes, my current PS1 is PS1='\u:\w\$ ' which I do not know what does mean. How should I change it to colorize command prompt to pink with out loosing current settings? – Yasser Zamani May 8 '12 at 17:40
9

@Rob is right; specifically, to change to light red (pink is not an available color):

PS1 = "\[\033[1;31m\]\u:\w\$\[\033[0m\] "
#      ^^^^^^^     ^^                    Begin/end ANSI escape
#             ^^^^^                      "light red foreground"
#                    ^^^^^^^             Your original prompt
#                           ^^^^^^^^^^^  Reset color back to default foreground

You need to use ANSI escape sequences (in this case 1;31m, the code for "light red foreground") which are enclosed by \[\033[ and \].

Edit: Light purple may be closer to your desired color; the PS1 change is left as an exercise for the reader.

reference @ the linux documentation project

  • 1
    I'm not great with ansi codes, so I left the answer to someone else. – Rob May 8 '12 at 21:53
  • ANSI escape sequences begin with \033 and go up to (including) m. The surrounding \[ and \] are bash-specific and are required so that bash can keep track of where the cursor is (it expects anything inside these markers not to advance the cursor). – egmont Apr 25 '15 at 22:45
0

If you would like an alternate solution you can create a small script for setting the terminal session PS1 colors without making them permanent (unless you wish for permanent colors follow Alex's solution).

If we create yellow.sh and save to /usr/bin directory, and then make the file executable with chmod +x yellow.sh we can then edit the yellow.sh and add the following code:

#!/bin/bash
clear

# Yellow color variable
ylw='\x1b[93m'

echo -e "${ylw}"
clear

Running this script will then change your terminal color to yellow. You can reset your PS1 color in the same way using this code:

#!/bin/bash
clear

# Reset color to default
rst='\x1b[0m'

echo -e "${rst}"
clear

Here is a quick list of some color-code variables you can use.

black=$'\x1b[90m' # Dark gray. Not used much.
red=$'\x1b[91m'
green=$'\x1b[92m'
yellow=$'\x1b[93m'
blue=$'\x1b[94m'
purple=$'\x1b[95m'
cyan=$'\x1b[96m'
pink=$'\x1b[97m'
default=$'\x1b[0m' # Reset to default color.

Usage for common commands inside simple scripts:

echo -e "${red} $(ls $1) ${default}" # Argument would be the directory you want to list.

This can be done rather quickly depending on how you setup your colors to be used. The color codes I have provided are not all. Depending on what colors your terminal supports, you could use a range of 256 colors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.