Color effects shell script of the show and the actual color PS1

In the bash shell scripts we can use ASCII text color to display information.

Format: \ 033 \ [31m hello \ 033 [0m

  ##m:

    The left #: # This may be 3 or 4, the role is not the same.

      3: foreground

      4: Background color

    On the right side #: Color Category

      1,2,3,4,5,6,7

    Foreground and the background using: \ 033 [## ; ## m Hello \ 033 [0m

  #m:

    Bold, flashing and other functions.

  A plurality of control symbols, may be used in combination with each other separated by semicolons.

Demo:

Demos background color

Using both foreground and background

Bold, flashing function

 

Practical exercise:

To their Linux-PS1 variable to add color, such as:

1, the first is to add value we give part of their PS1 color to be displayed.

 export PS1='[\033[31m\u\033[0m@\033[32m\h\033[0m \033[35m\W\033[0m]\$'

Set up this way is really good, met our needs, but he has a problem, that is, for example, when you enter a long string, you will find bash can not wrap show, but covers the beginning of the same line part, slowly back overwrite the contents of a start input, which is very optimistic, otherwise when we entered is too long, the display content is a problem how to live?

Preliminary judgment is to add a color code before such a problem, which may be in the PS1 environment variable, color code is not suitable for direct addition to PS1, so we need to be corrected, that is, after each color code plus \ [ with\] .

E.g:

export PS1='[\[\033[31m\]\u\[\033[0m\]@\[\033[32m\]\h\[\033[0m\] \[\033[35m\]\W\[\033[0m\]]\$'

It can be seen how many will color code plus the number of \ [and \] job. After this sub-set, this will be no problems, we continue to be happy to enter a long command in a terminal.

发布了10 篇原创文章 · 获赞 0 · 访问量 199

Guess you like

Origin blog.csdn.net/andy_f/article/details/104730356