Shell-colored command line

Source: Internet
Author: User
Color command line
 

Overview

This document uses bash shell as an example. (Escape sequences in different terminals are not exactly the same ).

Shell configuration

Shell color configuration appears in the bash Personal Configuration File~ /. BashrcOr a global configuration file./Etc/bashrc. You can useBashrcTo set the appearance of the prompt.

For example:

PS1="/s-/v/$ "
/S indicates the Shell Name, and-/V indicates the version number. Put a $ at the end of the prompt.
PS1="/u@/h /w /$ "

Indicates the user @ current directory $

Escape Sequence

If you use a colored prompt to increase personalization, you need to use escape sequences. The escape sequence is a control command that allows shell to execute a special step. Generally, escape sequences start with ESC (which is also the reason for its naming ). In shell, it is represented as ^ [. You can also use/033 to complete the same work (the ASCII code of ESC is expressed as 27 in decimal format and 33 in octal format ).

To directly input the escape sequence in shell, press Ctrl-V:CTRL-V ESC.

Use shell color

The following example uses a prompt to explain the shell color.

PS1="/[/033[0;32;40m/u@/h:/w/$ /]"
In this way, all the prompts are displayed in green. Like this:

Nico @ ebrain :~ $

/033 declares the start of the escape sequence, and then [begins to define the color. The following 0 defines the default font width. I will introduce other available characters later. Escape Sequence strings must be enclosed by/[and/] to prevent the text of escape sequences from occupying too much space in shell.

Select the foreground color (32 in green ). 40 of the background color indicates black. If you do not want to change the text after the prompt to green, you need to disable the escape sequence with/033 [0 m, And/033 [0 m is the default color of shell. Both the foreground and background colors are available.

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.

Example:

PS1 = "/[/033 [0; 37; 44 m/u @/033 [0; 32; 43 m/h:/033 [0; 33; 41 m/w $/033 [0 m/]"
This gives a very colorful prompt:

Nico @ Ebrain: ~ $


UseExport PS1 = "string"To test these settings;

PS1="/[/033[1;34;40m[/033[1;31;40m/u@/h:/w/033[1;34;40m]/033[1;37;40m $/033[0;37;0m/] "

[NICO @ ebrain :~ ]

 

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, meaningful values and mappings are as follows:
0. Default Value
1. Bold
22. Non-bold
4. Underline
24. Non-underline
5. Flashing
25. Non-blinking
7. Reverse display
27. Non-reverse display

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
Another program

Setting colors in shell is not only helpful for creating more beautiful prompts, but also useful for compiling console programs.

For a programmer who wants to use color, it must use a similarSlangOrNcursesSuch a library usually increases the size of the execution file.NcursesIt has the advantage of more or less independent terminal types.

C language example

Print "Hello word" in Green ":

#i nclude <stdio.h>
int main(void){
const char *const green = "/033[0;40;32m";
const char *const normal = "/033[0m";
printf("%sHello World%s/n", green, normal);
return 0;
}

Another useful escape sequence isPrintf ("/033 [2j "), It andSystem (clear)The completed functions are the same. However, you do not need header files.Unistd. h.

UsePrintf ("/033 [1 K ")You can delete a row.

Initialization Script example

If you want/Etc/init. dOfInitAfter the script is successfully executed, get a pretty clear and easy-to-read prompt, instead of a simple'.', You can use the escape sequence again.

This isCron init scriptExcerpt:

#!/bin/sh
# Start/stop the cron daemon.
test -f /usr/sbin/cron || exit 0

case "$1" in
start) echo -n "Starting periodic command scheduler: cron"
start-stop-daemon --start --quiet --exec /usr/sbin/cron

echo "."
;;

IfCronAfter the script is successfully executed, a period is displayed. You can use [OK] to add color features to the information.EchoString, for example:

#!/bin/sh
# Start/stop the cron daemon.
test -f /usr/sbin/cron || exit 0
case "$1" in
start) echo -n "Starting periodic command scheduler: cron"
start-stop-daemon --start --quiet --exec /usr/sbin/cron
echo "/[ /033[1;34;40m[ /033[1;32;40mOk /033[1;34;40m]/033[0m/]"
;;

Apply this setting to allInitScript time-consuming, unless escape sequence/033 is used -- becauseCTRL-VIt is not processed as a character.

 

Custom Command Prompt

We can customize the bash command prompt to display, including the current user name and host name, current time, average load and current working directory. To achieve this, modify the $ PS1 variable as follows:

Bash> ps1 = 'U @ H: W @>'
Bash> export PS1
Root @ Medusa:/tmp 0:01 PM>

The user name, host name, current working directory, and current time of the Current logon are displayed on the command line. You can obtain a list of symbols that Bash can understand from the user guide page.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.