Bash/Colors: Unterschied zwischen den Versionen

Aus Foxwiki
Keine Bearbeitungszusammenfassung
K Textersetzung - „[[Kategorie:/“ durch „[[Kategorie:“
 
(15 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 196: Zeile 196:
* https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
* https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux


 
[[Kategorie:Bash/Konfiguration]]
 
 
 
 
 
= Bash tips: Colors and formatting (ANSI/VT100 Control sequences) =
 
The '''ANSI/VT100''' terminals and terminal emulators are not just able to display black and white text ; they can display '''colors''' and formatted texts thanks to '''escape sequences'''. Those sequences are composed of the '''Escape character''' (often represented by “<tt>^[</tt>” or “<tt><Esc></tt>”) followed by some other characters: “<tt><Esc>[</tt><span style="color:#ff0000;">''FormatCode''</span><tt>m</tt>”.
 
In Bash, the <tt><Esc></tt> character can be obtained with the following syntaxes: * <tt>\e</tt>
* <tt>\033</tt>
* <tt>\x1B</tt>
 
 
 
''Examples:''
 
 
{| style="border-spacing:0;width:10.92cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code (Bash)
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| echo -e "\e[31mHello World\e[0m"
|| [[Image:Bild1.png|top|alt="Hello World"]]
|- style="border:none;padding:0.049cm;"
|| echo -e "\033[31mHello\e[0m World"
|| [[Image:Bild2.png|top|alt="Hello World"]]
|-
|}
''NOTE¹:'' The <tt>-e</tt> option of the <tt>echo</tt> command enable the parsing of the escape sequences.
 
''NOTE²:'' The “<tt>\e[0m</tt>” sequence removes all attributes (formatting and colors). It can be a good idea to add it at the end of each colored text. ;)
 
''NOTE³:'' The examples in this page are in '''Bash''' but the '''ANSI/VT100''' escape sequences can be used in every programming languages.
 
== Formatting ==
 
Here are the most commonly supported control sequences for formatting text. Their support depends on the used terminal ([https://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility see the compatibility list]).
 
=== Set ===
 
 
{| style="border-spacing:0;width:17cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code
! align=center| Description
! align=center| Example
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| 1
|| Bold/Bright
|| echo -e "Normal \e[1mBold"
|| [[Image:Bild3.png|top|alt="Normal Bold"]]
|- style="border:none;padding:0.049cm;"
|| 2
|| Dim
|| echo -e "Normal \e[2mDim"
|| [[Image:Bild4.png|top|alt="Normal Dim"]]
|- style="border:none;padding:0.049cm;"
|| 4
|| Underlined
|| echo -e "Normal \e[4mUnderlined"
|| [[Image:Bild5.png|top|alt="Normal Underlined"]]
|- style="border:none;padding:0.049cm;"
|| 5
|| Blink [https://misc.flogisoft.com/bash/tip_colors_and_formatting#fn__1 1)]  
|| echo -e "Normal \e[5mBlink"
|| [[Image:Bild6.png|top|alt="Normal Blink"]]
|- style="border:none;padding:0.049cm;"
|| 7
|| Reverse (invert the foreground and background colors)
|| echo -e "Normal \e[7minverted"
|| [[Image:Bild7.png|top|alt="Normal inverted"]]
|- style="border:none;padding:0.049cm;"
|| 8
|| Hidden (useful for passwords)
|| echo -e "Normal \e[8mHidden"
|| [[Image:Bild8.png|top|alt="Normal Hidden"]]
 
 
|-
|}
=== Reset ===
 
 
{| style="border-spacing:0;width:20.253cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code
! align=center| Description
! align=center| Example
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| 0
|| Reset all attributes
|| echo -e "\e[0mNormal Text"
|| [[Image:Bild9.png|top|alt="Normal Text"]]
|- style="border:none;padding:0.049cm;"
|| 21
|| Reset bold/bright
|| echo -e "Normal \e[1mBold \e[21mNormal"
|| [[Image:Bild10.png|top|alt="Normal Bold Normal"]]
|- style="border:none;padding:0.049cm;"
|| 22
|| Reset dim
|| echo -e "Normal \e[2mDim \e[22mNormal"
|| [[Image:Bild11.png|top|alt="Normal Dim Normal"]]
|- style="border:none;padding:0.049cm;"
|| 24
|| Reset underlined
|| echo -e "Normal \e[4mUnderlined \e[24mNormal"
|| [[Image:Bild12.png|top|alt="Normal Underlined Normal"]]
|- style="border:none;padding:0.049cm;"
|| 25
|| Reset blink
|| echo -e "Normal \e[5mBlink \e[25mNormal"
|| [[Image:Bild13.png|top|alt="Normal Blink Normal"]]
|- style="border:none;padding:0.049cm;"
|| 27
|| Reset reverse
|| echo -e "Normal \e[7minverted \e[27mNormal"
|| [[Image:Bild14.png|top|alt="Normal inverted Normal"]]
|- style="border:none;padding:0.049cm;"
|| 28
|| Reset hidden
|| echo -e "Normal \e[8mHidden \e[28mNormal"
|| [[Image:Bild15.png|top|alt="Normal Hidden Normal"]]
 
 
|-
|}
== 8/16 Colors ==
 
The following colors works with most terminals and terminals emulators [https://misc.flogisoft.com/bash/tip_colors_and_formatting#fn__2 2)], [https://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility see the compatibility list] for more informations.
 
''NOTE:'' The colors can vary depending of the terminal configuration.
 
=== Foreground (text) ===
 
 
{| style="border-spacing:0;width:17.501cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code
! align=center| Color
! align=center| Example
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| 39
|| Default foreground color
|| echo -e "Default \e[39mDefault"
|| [[Image:Bild16.png|top|alt="Default Default"]]
|- style="border:none;padding:0.049cm;"
|| 30
|| Black
|| echo -e "Default \e[30mBlack"
|| [[Image:Bild17.png|top|alt="Default Black"]]
|- style="border:none;padding:0.049cm;"
|| 31
|| Red
|| echo -e "Default \e[31mRed"
|| [[Image:Bild18.png|top|alt="Default Red"]]
|- style="border:none;padding:0.049cm;"
|| 32
|| Green
|| echo -e "Default \e[32mGreen"
|| [[Image:Bild19.png|top|alt="Default Green"]]
|- style="border:none;padding:0.049cm;"
|| 33
|| Yellow
|| echo -e "Default \e[33mYellow"
|| [[Image:Bild20.png|top|alt="Default Yellow"]]
|- style="border:none;padding:0.049cm;"
|| 34
|| Blue
|| echo -e "Default \e[34mBlue"
|| [[Image:Bild21.png|top|alt="Default Blue"]]
|- style="border:none;padding:0.049cm;"
|| 35
|| Magenta
|| echo -e "Default \e[35mMagenta"
|| [[Image:Bild22.png|top|alt="Default Magenta"]]
|- style="border:none;padding:0.049cm;"
|| 36
|| Cyan
|| echo -e "Default \e[36mCyan"
|| [[Image:Bild23.png|top|alt="Default Cyan"]]
|- style="border:none;padding:0.049cm;"
|| 37
|| Light gray
|| echo -e "Default \e[37mLight gray"
|| [[Image:Bild24.png|top|alt="Default Light gray"]]
|- style="border:none;padding:0.049cm;"
|| 90
|| Dark gray
|| echo -e "Default \e[90mDark gray"
|| [[Image:Bild25.png|top|alt="Default Dark gray"]]
|- style="border:none;padding:0.049cm;"
|| 91
|| Light red
|| echo -e "Default \e[91mLight red"
|| [[Image:Bild26.png|top|alt="Default Light red"]]
|- style="border:none;padding:0.049cm;"
|| 92
|| Light green
|| echo -e "Default \e[92mLight green"
|| [[Image:Bild27.png|top|alt="Default Light green"]]
|- style="border:none;padding:0.049cm;"
|| 93
|| Light yellow
|| echo -e "Default \e[93mLight yellow"
|| [[Image:Bild28.png|top|alt="Default Light yellow"]]
|- style="border:none;padding:0.049cm;"
|| 94
|| Light blue
|| echo -e "Default \e[94mLight blue"
|| [[Image:Bild29.png|top|alt="Default Light blue"]]
|- style="border:none;padding:0.049cm;"
|| 95
|| Light magenta
|| echo -e "Default \e[95mLight magenta"
|| [[Image:Bild30.png|top|alt="Default Light magenta"]]
|- style="border:none;padding:0.049cm;"
|| 96
|| Light cyan
|| echo -e "Default \e[96mLight cyan"
|| [[Image:Bild31.png|top|alt="Default Light cyan"]]
|- style="border:none;padding:0.049cm;"
|| 97
|| White
|| echo -e "Default \e[97mWhite"
|| [[Image:Bild32.png|top|alt="Default White"]]
 
 
|-
|}
=== Background ===
 
 
{| style="border-spacing:0;width:17.794cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code
! align=center| Color
! align=center| Example
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| 49
|| Default background color
|| echo -e "Default \e[49mDefault"
|| [[Image:Bild33.png|top|alt="Default Default"]]
|- style="border:none;padding:0.049cm;"
|| 40
|| Black
|| echo -e "Default \e[40mBlack"
|| [[Image:Bild34.png|top|alt="Default Black"]]
|- style="border:none;padding:0.049cm;"
|| 41
|| Red
|| echo -e "Default \e[41mRed"
|| [[Image:Bild35.png|top|alt="Default Red"]]
|- style="border:none;padding:0.049cm;"
|| 42
|| Green
|| echo -e "Default \e[42mGreen"
|| [[Image:Bild36.png|top|alt="Default Green"]]
|- style="border:none;padding:0.049cm;"
|| 43
|| Yellow
|| echo -e "Default \e[43mYellow"
|| [[Image:Bild37.png|top|alt="Default Yellow"]]
|- style="border:none;padding:0.049cm;"
|| 44
|| Blue
|| echo -e "Default \e[44mBlue"
|| [[Image:Bild38.png|top|alt="Default Blue"]]
|- style="border:none;padding:0.049cm;"
|| 45
|| Magenta
|| echo -e "Default \e[45mMagenta"
|| [[Image:Bild39.png|top|alt="Default Magenta"]]
|- style="border:none;padding:0.049cm;"
|| 46
|| Cyan
|| echo -e "Default \e[46mCyan"
|| [[Image:Bild40.png|top|alt="Default Cyan"]]
|- style="border:none;padding:0.049cm;"
|| 47
|| Light gray
|| echo -e "Default \e[47mLight gray"
|| [[Image:Bild41.png|top|alt="Default Light gray"]]
|- style="border:none;padding:0.049cm;"
|| 100
|| Dark gray
|| echo -e "Default \e[100mDark gray"
|| [[Image:Bild42.png|top|alt="Default Dark gray"]]
|- style="border:none;padding:0.049cm;"
|| 101
|| Light red
|| echo -e "Default \e[101mLight red"
|| [[Image:Bild43.png|top|alt="Default Light red"]]
|- style="border:none;padding:0.049cm;"
|| 102
|| Light green
|| echo -e "Default \e[102mLight green"
|| [[Image:Bild44.png|top|alt="Default Light green"]]
|- style="border:none;padding:0.049cm;"
|| 103
|| Light yellow
|| echo -e "Default \e[103mLight yellow"
|| [[Image:Bild45.png|top|alt="Default Light yellow"]]
|- style="border:none;padding:0.049cm;"
|| 104
|| Light blue
|| echo -e "Default \e[104mLight blue"
|| [[Image:Bild46.png|top|alt="Default Light blue"]]
|- style="border:none;padding:0.049cm;"
|| 105
|| Light magenta
|| echo -e "Default \e[105mLight magenta"
|| [[Image:Bild47.png|top|alt="Default Light magenta"]]
|- style="border:none;padding:0.049cm;"
|| 106
|| Light cyan
|| echo -e "Default \e[106mLight cyan"
|| [[Image:Bild48.png|top|alt="Default Light cyan"]]
|- style="border:none;padding:0.049cm;"
|| 107
|| White
|| echo -e "Default \e[107mWhite"
|| [[Image:Bild49.png|top|alt="Default White"]]
 
 
|-
|}
== 88/256 Colors ==
 
Some terminals ([https://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility see the compatibility list]) can support 88 or 256 colors. Here are the control sequences that permit you to use them.
 
''NOTE¹'': The colors number <tt>256</tt> is only supported by '''vte''' (GNOME Terminal, XFCE4 Terminal, Nautilus Terminal, Terminator,…).
 
''NOTE²'': The 88-colors terminals (like '''rxvt''') does not have the same color map that the 256-colors terminals. For showing the 88-colors terminals color map, run the “[https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors2 256-colors.sh]” script in a 88-colors terminal.
 
=== Foreground (text) ===
 
For using one of the 256 colors on the foreground (text color), the control sequence is “<tt><Esc>[38;5;</tt><span style="color:#ff0000;">''ColorNumber''</span><tt>m</tt>” where <tt>ColorNumber</tt> is one of the following colors:
 
[[Image:Bild50.png|top|alt="XTerm 256 color list (foreground)"]]
 
''Examples:''
 
 
{| style="border-spacing:0;width:20.024cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code (Bash)
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| echo -e "\e[38;5;82mHello \e[38;5;198mWorld"
|| [[Image:Bild51.png|top|alt="Hello World"]]
|- style="border:none;padding:0.049cm;"
|| for i in {16..21} {21..16} ; do echo -en "\e[38;5;${i}m#\e[0m" ; done ; echo
|| [[Image:Bild52.png|top|alt="Blue gradiant"]]
 
 
|-
|}
=== Background ===
 
For using one of the 256 colors on the background, the control sequence is “<tt><Esc>[48;5;</tt><span style="color:#ff0000;">''ColorNumber''</span><tt>m</tt>” where <tt>ColorNumber</tt> is one of the following colors:
 
[[Image:Bild53.png|top|alt="XTerm 256 color list (background)"]]
 
''Examples:''
 
 
{| style="border-spacing:0;width:20.516cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Code (Bash)
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| echo -e "\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m"
|| [[Image:Bild54.png|top|alt="Hello World"]]
|- style="border:none;padding:0.049cm;"
|| for i in {16..21} {21..16} ; do echo -en "\e[48;5;${i}m \e[0m" ; done ; echo
|| [[Image:Bild55.png|top|alt="Blue gradiant"]]
 
 
|-
|}
== Attributes combination ==
 
Terminals allow attribute combinations. The attributes must be separated by a semicolon (“<tt><nowiki>;</nowiki></tt>”).
 
''Examples:''
 
 
{| style="border-spacing:0;width:17.341cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Description
! align=center| Code (Bash)
! align=center| Preview
|- style="border:none;padding:0.049cm;"
|| Bold + Underlined
|| echo -e "\e[1;4mBold and Underlined"
|| [[Image:Bild56.png|top|alt="Bold and Underlined"]]
|- style="border:none;padding:0.049cm;"
|| Bold + Red forground + Green background
|| echo -e "\e[1;31;42m Yes it is awful \e[0m"
|| [[Image:Bild57.png|top|alt="Yes it is awful"]]
 
 
|-
|}
== Terminals compatibility ==
 
 
{| style="border-spacing:0;width:17cm;"
|- style="border:none;padding:0.049cm;"
! align=center| Terminal
!colspan="6" |  align=center| Formatting
!colspan="4" |  align=center| Colors
! align=center| Comment
|- style="border:none;padding:0.049cm;"
! align=center| Bold
! align=center| Dim
! align=center| Underlined
! align=center| Blink
! align=center| invert
! align=center| Hidden
! align=center| 8
! align=center| 16
! align=center| 88
! align=center| 256
|- style="border:none;padding:0.049cm;"
|| [http://www.afterstep.org/aterm.php aTerm]
|| ok
|| -
|| ok
|| -
|| ok
|| -
|| ok
|| ~
|| -
|| -
|| Lighter background instead of blink.
|- style="border:none;padding:0.049cm;"
|| [http://www.eterm.org/ Eterm]
|| ~
|| -
|| ok
|| -
|| ok
|| -
|| ok
|| ~
|| -
|| ok
|| Lighter color instead of Bold. Lighter background instead of blink. Can overline a text with the “<tt><nowiki>^[[6m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://library.gnome.org/users/gnome-terminal/ GNOME Terminal]
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://guake.org/ Guake]
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://konsole.kde.org/ Konsole]
|| ok
|| -
|| ok
|| ok
|| ok
|| -
|| ok
|| ok
|| -
|| ok
||
|- style="border:none;padding:0.049cm;"
|| [https://github.com/flozz/nautilus-terminal Nautilus Terminal]
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://rxvt.sourceforge.net/ rxvt]
|| ok
|| -
|| ok
|| ~
|| ok
|| -
|| ok
|| ok
|| ok
|| -
|| If the background is not set to the default color, Blink make it lighter instead of blinking. Support of italic text with the “<tt><nowiki>^[[3m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://www.tenshu.net/terminator/ Terminator]
|| ok
|| ok
|| ok
|| -
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://tilda.sourceforge.net/tildaabout.php Tilda]
|| ok
|| -
|| ok
|| ok
|| ok
|| -
|| ok
|| ok
|| -
|| -
|| Underline instead of Dim. Convert 256-colors in 16-colors.
|- style="border:none;padding:0.049cm;"
|| [http://www.xfce.org/projects/terminal XFCE4 Terminal]
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|- style="border:none;padding:0.049cm;"
|| [http://invisible-island.net/xterm/xterm.html XTerm]
|| ok
|| -
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
||
|- style="border:none;padding:0.049cm;"
|| xvt
|| ok
|| -
|| ok
|| -
|| ok
|| -
|| -
|| -
|| -
|| -
||
|- style="border:none;padding:0.049cm;"
|| Linux TTY
|| ok
|| -
|| -
|| -
|| ok
|| -
|| ok
|| ~
|| -
|| -
|| Specials colors instead of Dim and Underlined. Lighter background instead of Blink, Bug with 88/256 colors.
|- style="border:none;padding:0.049cm;"
|| [http://developer.gnome.org/vte/ VTE Terminal] [https://misc.flogisoft.com/bash/tip_colors_and_formatting#fn__3 3)]
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| ok
|| -
|| ok
|| Strikeout with the “<tt><nowiki>^[[9m</nowiki></tt>” sequence.
|-
|}
''Notations used in the table:'' * “<tt>ok</tt>”: Supported by the terminal.
* “<tt>~</tt>”: Supported in a special way by the terminal.
* “<tt>-</tt>”: Not supported at all by the terminal.
 
 
 
== Demonstration programs ==
 
=== Colors and formatting (16 colors) ===
 
[[Image:Bild58.png|top|alt="Screenshot of the color_and_formatting.sh script"]]
 
The following shell script displays a lot of possible combination of the attributes (but not all, because it uses only one formatting attribute at a time).
 
[https://misc.flogisoft.com/_export/code/bash/tip_colors_and_formatting?codeblock=55 colors_and_formatting.sh]
 
<div style="margin-left:1cm;margin-right:0cm;"><nowiki>#!/bin/bash</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># This program is free software. It comes without any warranty, to</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># the extent permitted by applicable law. You can redistribute it</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># and/or modify it under the terms of the Do What The Fuck You Want</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># To Public License, Version 2, as published by Sam Hocevar. See</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># http://sam.zoy.org/wtfpl/COPYING for more details.</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki>#Background</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">for clbg in {40..47} {100..107} 49 ; do</div>
<div style="margin-left:1cm;margin-right:0cm;">        <nowiki>#Foreground</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">        for clfg in {30..37} {90..97} 39 ; do</div>
<div style="margin-left:1cm;margin-right:0cm;">                <nowiki>#Formatting</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">                for attr in 0 1 2 4 5 7 ; do</div>
<div style="margin-left:1cm;margin-right:0cm;">                        <nowiki>#Print the result</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">                        echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m"</div>
<div style="margin-left:1cm;margin-right:0cm;">                done</div>
<div style="margin-left:1cm;margin-right:0cm;">                echo #Newline</div>
<div style="margin-left:1cm;margin-right:0cm;">        done</div>
<div style="margin-left:1cm;margin-right:0cm;">done</div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;">exit 0</div>
 
=== 256 colors ===
 
[[Image:Bild59.png|top|alt="Screenshot of the 256-colors.sh script"]]
 
The following script display the 256 colors available on some terminals and terminals emulators like '''XTerm''' and '''GNOME Terminal'''.
 
[https://misc.flogisoft.com/_export/code/bash/tip_colors_and_formatting?codeblock=56 256-colors.sh]
 
<div style="margin-left:1cm;margin-right:0cm;"><nowiki>#!/bin/bash</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># This program is free software. It comes without any warranty, to</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># the extent permitted by applicable law. You can redistribute it</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># and/or modify it under the terms of the Do What The Fuck You Want</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># To Public License, Version 2, as published by Sam Hocevar. See</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;"><nowiki># http://sam.zoy.org/wtfpl/COPYING for more details.</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;">for fgbg in 38 48 ; do # Foreground / Background</div>
<div style="margin-left:1cm;margin-right:0cm;">    for color in {0..255} ; do # Colors</div>
<div style="margin-left:1cm;margin-right:0cm;">        <nowiki># Display the color</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">        printf "\e[${fgbg};5;%sm  %3s  \e[0m" $color $color</div>
<div style="margin-left:1cm;margin-right:0cm;">        <nowiki># Display 6 colors per lines</nowiki></div>
<div style="margin-left:1cm;margin-right:0cm;">        if [ $((($color + 1) % 6)) == 4 ] ; then</div>
<div style="margin-left:1cm;margin-right:0cm;">            echo # New line</div>
<div style="margin-left:1cm;margin-right:0cm;">        fi</div>
<div style="margin-left:1cm;margin-right:0cm;">    done</div>
<div style="margin-left:1cm;margin-right:0cm;">    echo # New line</div>
<div style="margin-left:1cm;margin-right:0cm;">done</div>
<div style="margin-left:1cm;margin-right:0cm;">&nbsp;</div>
<div style="margin-left:1cm;margin-right:0cm;">exit 0</div>
 
== Links ==
 
* [http://linux.die.net/man/4/console_codes Linux console codes manual (''man console_codes'')]
* [http://invisible-island.net/xterm/ctlseqs/ctlseqs.html XTerm Control Sequences]
* [http://bjh21.me.uk/all-escapes/all-escapes.txt Compilation of all escape sequences]
* [https://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape code (Wikipedia)]
 
 
 
[https://misc.flogisoft.com/bash/tip_colors_and_formatting#fnt__1 1)]
 
Does not work with most of the terminal emulators, works in the tty and XTerm.
 
[https://misc.flogisoft.com/bash/tip_colors_and_formatting#fnt__2 2)]
 
Some terminals supports only the first 8 colors (30..37 and 40..47), and some others does not support any color at all.
 
[https://misc.flogisoft.com/bash/tip_colors_and_formatting#fnt__3 3)]  
 
GTK Widget used in GNOME Terminal, Nautilus Terminal, XFCE4 Terminal…

Aktuelle Version vom 28. März 2023, 10:37 Uhr

Bash Colors

You can make your BASH script more pretty, by colorizing its output.

Use ANSI escape sequences to set text properties like foreground and background colors.

Colorizing Shell

Use the following template for writing colored text:

echo -e "\e[COLORmSample Text\e[0m"


Option Description
-e Enable interpretation of backslash escapes
\e[ Begin the color modifications
COLORm Color Code + ‘m’ at the end
\e[0m End the color modifications

Examples:

$ echo -e "\e[31mRed Text\e[0m"

Red Text

$ echo -e "\e[42mGreen Background\e[0m" Green Background

ANSI — Color Escape Codes

Shell scripts commonly use ANSI escape codes for color output:


Color Foreground Code Background Code Sample
Black 30 40
Red 31 41
Green 32 42
Brown 33 43
Blue 34 44
Purple 35 45
Cyan 36 46
Light Gray 37 47


Escape sequence also allows to control the manner in which characters are displayed on the screen:


ANSI Code Description
0 Normal Characters
1 Bold Characters
4 Underlined Characters
5 Blinking Characters
7 Reverse video Characters


Examples:

$ echo -e "\e[1mBold Text\e[0m" Bold Text $ echo -e "\e[3mUnderlined Text\e[0m" Underlined Text

By combining all these escape sequences, we can get more fancy effect.

echo -e "\e[COLOR1;COLOR2mSample Text\e[0m"

There are some differences between colors when combining colors with bold text attribute:


Color Foreground Code Background Code Sample
Dark Gray 1;30 1;40
Light Red 1;31 1;41
Light Green 1;32 1;42
Yellow 1;33 1;43
Light Blue 1;34 1;44
Light Purple 1;35 1;45
Light Cyan 1;36 1;46
White 1;37 1;47


Examples:

$ echo -e "\e[1;34mLight Blue Text\e[0m"

Light Blue Text

$ echo -e "\e[1;33;4;44mYellow Underlined Text on Blue Background\e[0m"

Yellow Underlined Text on Blue Background

Links

Weblinks