In ANSI compatible terminals (such as xterm, rxvt, and konsole), texts can be displayed in color, not just in black and white. This article demonstrates how to use bold and colored text.
I believe that every linux user must face bash. Although it seems boring at first glance, there are many ways to make your shell personalized enough. The color enhancement of the shell prompt makes your shell unique and clear and easy to read.
In this article, I use bash shell as an example. Because the escape sequences in different terminals are not exactly the same, I use ANSI terminals in this article.
Shell color configuration appears in the bash Personal Configuration File
~ /. Bashrc
Or a global configuration file.
/Etc/bashrc
. You can useBashrc
To set the appearance of the prompt.
In general, the default. bashrc should be:
~ /. Bashrc
Export PS1 = '/u @/h:/W/$'
The above means: user @ host: Current directory $
--> This is also a shell prompt that everyone is very familiar.
Escape Sequence
Yes
The escape sequence is used to increase personalization through a colored prompt. The escape sequence is a control command that allows shell to execute a special step. The escape sequence is usually based on ESC.
(This is also the reason for its naming ). In shell, it is represented as ^ [. This representation takes a little time to adapt, or you can use/033 to complete the same work (ESC's
The ASCII code in decimal format is 27, which is equal to 033 in octal format ).
To directly input the escape sequence in the shell, press Ctrl-V:CTRL-V ESC
.
Next we will go to the topic and explain how to use shell colors. I used my own shell color settings for detailed instructions.
Code:
PS1 = "/[/033 [1; 32; 40 m [/033 [0; 32; 40 m/u @/h:/033 [1; 35; 40 m/w/033 [1; 32; 40 m]/033 [1; 31; 40 m/$/033 [1; 32; 40 m/]"
The outermost "/[/]" is used to enclose the character strings of the escape sequence,
Prevent text of escape sequences from occupying too much space in shell.
/033 declares the start of the escape sequence, and then [begins to define the color. The following 0 defines the default font width, followed by a number in the middle to define the character color. The last digit defines the character background color.
The letter M is necessary for definition, and the character after the letter M is the character you want to change.
Optional colors: Red, green, yellow, blue, magenta, cyan, and white. Their corresponding color codes are: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (Foreign red), 36 (blue), 37 (white ).
Set the background color using the same color method, but Replace the first digit "3" with "4", for example, 40, 41, 42, 43, 44, 45, 46, and 47.
For example, in my definition above: the final result is that all the characters before $ are expanded with a light green middle arc. The $ character is displayed in red. The subsequent input is highlighted in light green. In the arc, the user name @ host is displayed as dark green, and the path is displayed as foreign red.
Use code to explain:
PS1 = "/[-------------> outermost arc
/033 [1; 32; 40 m [
----> Define the rightmost "[";
1: font width, which seems to be highlighted.
32: Character Color. 32 indicates green.
40: background color. 40 indicates black.
/033 [0; 32; 40 m/u @/h:
---------> Define the/u @/h on the right: the color of these characters.
0: default font width.
32: Character Color. Green.
40: background color. Black.
/033 [1; 35; 40 m/w
---------> Define/W (path color) on the right ).
1: character width. Highlight.
35: Character Color. Yang Hong.
40: background color. Black.
/033 [1; 32; 40 m]
---------> Define] characters on the right. Use the same effect as the preceding [character.
/033 [1; 31; 40 m/$
---------> The $ character is defined as red. Escape characters, so /.
The colors are similar to those in the front. I believe everyone has discovered the rule.
/033 [1; 32; 40 m
----------> This defines the color that you want to enter in the command line.
/]"
-----------> The string ends the arc.
This is the detailed definition of the above example.
Its
In fact, I didn't turn off the color escape sequence, so that I could change the color of the characters I entered. It's cool to watch the black and white screen all day !! If you do not want to change the input color, use/033 [0 m
Turn off the escape sequence./033 [0 m is the default color of shell. In the above example, add/033 [0 m before.
Is it simple enough?
Let's look at the shell under root:
This is also good. It's from Nico Golde:
PS1 = "/[/033 [0; 37; 44 m/u @/033 [0; 32; 43 m/h:/033 [0; 33; 41 m/w $/033 [0 m/]"
Here there are more color combinations, all of which are implemented through the bash shell script. You can choose one of your favorite ones.
Text attributes
As mentioned earlier, "0" after the first escape sequence is the default color settings of the text in the prompt. For text attributes, these values are meaningful: 0, 1, 22, 4, 24, 5, 25, 7, and 27, respectively: default Value: bold, non-bold, underline, non-underline, flickering, non-blinking, reverse, non-reversed.
The following short script shows the color combinations.
#!/bin/sh
############################################################
# Nico Golde <nico(at)ngolde.de> Homepage: http://www.ngolde.de
# Last change: Mon Feb 16 16:24:41 CET 2004
############################################################
for attr in 0 1 4 5 7 ; do
echo "----------------------------------------------------------------"
printf "ESC[%s;Foreground;Background - /n" $attr
for fore in 30 31 32 33 34 35 36 37; do
for back in 40 41 42 43 44 45 46 47; do
printf '/033[%s;%s;%sm %02s;%02s ' $attr $fore $back $fore $back
done
printf '/n'
done
printf '/033[0m'
done