I americium making an attempt to mark a matter successful a reddish colour successful the terminal utilizing echo bid.
However tin I bash that?
You tin usage these ANSI flight codes:
Black 0;30 Dark Gray 1;30Red 0;31 Light Red 1;31Green 0;32 Light Green 1;32Brown/Orange 0;33 Yellow 1;33Blue 0;34 Light Blue 1;34Purple 0;35 Light Purple 1;35Cyan 0;36 Light Cyan 1;36Light Gray 0;37 White 1;37
And past usage them similar this successful your book:
# .---------- constant part!# vvvv vvvv-- the code from aboveRED='\033[0;31m'NC='\033[0m' # No Colorprintf "I ${RED}love${NC} Stack Overflow\n"
which prints love
successful reddish.
From @james-lim's remark, if you are utilizing the echo
bid, beryllium certain to usage the -e
emblem to let backslash escapes.
# .---------- constant part!# vvvv vvvv-- the code from aboveRED='\033[0;31m'NC='\033[0m' # No Colorecho -e "I ${RED}love${NC} Stack Overflow"
Line: Don't adhd "\n"
once utilizing echo
until you privation to adhd an further bare formation.
any variables that you tin usage:
# ResetColor_Off='\033[0m' # Text Reset# Regular ColorsBlack='\033[0;30m' # BlackRed='\033[0;31m' # RedGreen='\033[0;32m' # GreenYellow='\033[0;33m' # YellowBlue='\033[0;34m' # BluePurple='\033[0;35m' # PurpleCyan='\033[0;36m' # CyanWhite='\033[0;37m' # White# BoldBBlack='\033[1;30m' # BlackBRed='\033[1;31m' # RedBGreen='\033[1;32m' # GreenBYellow='\033[1;33m' # YellowBBlue='\033[1;34m' # BlueBPurple='\033[1;35m' # PurpleBCyan='\033[1;36m' # CyanBWhite='\033[1;37m' # White# UnderlineUBlack='\033[4;30m' # BlackURed='\033[4;31m' # RedUGreen='\033[4;32m' # GreenUYellow='\033[4;33m' # YellowUBlue='\033[4;34m' # BlueUPurple='\033[4;35m' # PurpleUCyan='\033[4;36m' # CyanUWhite='\033[4;37m' # White# BackgroundOn_Black='\033[40m' # BlackOn_Red='\033[41m' # RedOn_Green='\033[42m' # GreenOn_Yellow='\033[43m' # YellowOn_Blue='\033[44m' # BlueOn_Purple='\033[45m' # PurpleOn_Cyan='\033[46m' # CyanOn_White='\033[47m' # White# High IntensityIBlack='\033[0;90m' # BlackIRed='\033[0;91m' # RedIGreen='\033[0;92m' # GreenIYellow='\033[0;93m' # YellowIBlue='\033[0;94m' # BlueIPurple='\033[0;95m' # PurpleICyan='\033[0;96m' # CyanIWhite='\033[0;97m' # White# Bold High IntensityBIBlack='\033[1;90m' # BlackBIRed='\033[1;91m' # RedBIGreen='\033[1;92m' # GreenBIYellow='\033[1;93m' # YellowBIBlue='\033[1;94m' # BlueBIPurple='\033[1;95m' # PurpleBICyan='\033[1;96m' # CyanBIWhite='\033[1;97m' # White# High Intensity backgroundsOn_IBlack='\033[0;100m' # BlackOn_IRed='\033[0;101m' # RedOn_IGreen='\033[0;102m' # GreenOn_IYellow='\033[0;103m' # YellowOn_IBlue='\033[0;104m' # BlueOn_IPurple='\033[0;105m' # PurpleOn_ICyan='\033[0;106m' # CyanOn_IWhite='\033[0;107m' # White
the flight quality successful bash, hex and octal respectively:
| | bash | hex | octal | NOTE ||-------+-------+---------+---------+------------------------------|| start | \e | \x1b | \033 | || start | \E | \x1B | - | x cannot be capital || end | \e[0m | \x1b[0m | \033[0m | || end | \e[m | \x1b[m | \033[m | 0 is appended if you omit it || | | | | |
abbreviated illustration:
| color | bash | hex | octal | NOTE ||-------------+--------------+----------------+----------------+---------------------------------------|| start green | \e[32m<text> | \x1b[32m<text> | \033[32m<text> | m is NOT optional || reset | <text>\e[0m | <text>\1xb[0m | <text>\033[om | o is optional (do it as best practice || | | | | |
bash objection:
If you are going to usage these codes successful your particular bash variables
- PS0
- PS1
- PS2 (= this is for prompting)
- PS4
you ought to adhd other flight characters truthful that bash tin construe them appropriately. With out this including other flight characters it plant however you volition expression issues once you usage Ctrl + r
for hunt successful your past.
objection regulation for bash
You ought to adhd \[
earlier immoderate beginning ANSI codification and adhd \]
last immoderate ending ones.
Illustration:
successful daily utilization: \033[32mThis is in green\033[0m
for PS0/1/2/Four: \[\033[32m\]This is in green\[\033[m\]
\[
is for commencement of a series of non-printable characters\]
is for extremity of a series of non-printable characters
End: for memorize it you tin archetypal adhd \[\]
and past option your ANSI codification betwixt them:
\[start-ANSI-code\]
\[end-ANSI-code\]
kind of colour series:
- Three/Four spot
- Eight spot
- 24 spot
Earlier diving into these colours, you ought to cognize astir Four modes with these codes:
1. colour-manner
It modifies the kind of colour NOT matter. For illustration brand the colour agleam oregon darker.
0
reset1;
lighter than average2;
darker than average
This manner is not supported wide. It is full activity connected Gnome-Terminal.
2. matter-manner
This manner is for modifying the kind of matter NOT colour.
3;
italic4;
underline5;
blinking (dilatory)6;
blinking (accelerated)7;
reverse8;
fell9;
transverse-retired
and are about supported.
For illustration KDE-Konsole helps 5;
however Gnome-Terminal does not and Gnome helps 8;
however KDE does not.
Three. foreground manner
This manner is for colorizing the foreground.
Four. inheritance manner
This manner is for colorizing the inheritance.
The beneath array exhibits a abstract of Three/Four spot interpretation of ANSI-colour
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| color-mode | octal | hex | bash | description | example (= in octal) | NOTE ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 0 | \033[0m | \x1b[0m | \e[0m | reset any affect | echo -e "\033[0m" | 0m equals to m || 1 | \033[1m | | | light (= bright) | echo -e "\033[1m####\033[m" | - || 2 | \033[2m | | | dark (= fade) | echo -e "\033[2m####\033[m" | - ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| text-mode | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 3 | \033[3m | | | italic | echo -e "\033[3m####\033[m" | || 4 | \033[4m | | | underline | echo -e "\033[4m####\033[m" | || 5 | \033[5m | | | blink (slow) | echo -e "\033[3m####\033[m" | || 6 | \033[6m | | | blink (fast) | ? | not wildly support || 7 | \003[7m | | | reverse | echo -e "\033[7m####\033[m" | it affects the background/foreground || 8 | \033[8m | | | hide | echo -e "\033[8m####\033[m" | it affects the background/foreground || 9 | \033[9m | | | cross | echo -e "\033[9m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| foreground | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 30 | \033[30m | | | black | echo -e "\033[30m####\033[m" | || 31 | \033[31m | | | red | echo -e "\033[31m####\033[m" | || 32 | \033[32m | | | green | echo -e "\033[32m####\033[m" | || 33 | \033[33m | | | yellow | echo -e "\033[33m####\033[m" | || 34 | \033[34m | | | blue | echo -e "\033[34m####\033[m" | || 35 | \033[35m | | | purple | echo -e "\033[35m####\033[m" | real name: magenta = reddish-purple || 36 | \033[36m | | | cyan | echo -e "\033[36m####\033[m" | || 37 | \033[37m | | | white | echo -e "\033[37m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 38 | 8/24 | This is for special use of 8-bit or 24-bit ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| background | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 40 | \033[40m | | | black | echo -e "\033[40m####\033[m" | || 41 | \033[41m | | | red | echo -e "\033[41m####\033[m" | || 42 | \033[42m | | | green | echo -e "\033[42m####\033[m" | || 43 | \033[43m | | | yellow | echo -e "\033[43m####\033[m" | || 44 | \033[44m | | | blue | echo -e "\033[44m####\033[m" | || 45 | \033[45m | | | purple | echo -e "\033[45m####\033[m" | real name: magenta = reddish-purple || 46 | \033[46m | | | cyan | echo -e "\033[46m####\033[m" | || 47 | \033[47m | | | white | echo -e "\033[47m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 48 | 8/24 | This is for special use of 8-bit or 24-bit | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
The beneath array exhibits a abstract of Eight spot interpretation of ANSI-colour
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| foreground | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| 0-7 | \033[38;5 | \x1b[38;5 | \e[38;5 | standard. normal | echo -e '\033[38;5;1m####\033[m' | || 8-15 | | | | standard. light | echo -e '\033[38;5;9m####\033[m' | || 16-231 | | | | more resolution | echo -e '\033[38;5;45m####\033[m' | has no specific pattern || 232-255 | | | | | echo -e '\033[38;5;242m####\033[m' | from black to white ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| background | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| 0-7 | | | | standard. normal | echo -e '\033[48;5;1m####\033[m' | || 8-15 | | | | standard. light | echo -e '\033[48;5;9m####\033[m' | || 16-231 | | | | more resolution | echo -e '\033[48;5;45m####\033[m' | || 232-255 | | | | | echo -e '\033[48;5;242m####\033[m' | from black to white ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
The Eight-spot accelerated trial:for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done
The beneath array exhibits a abstract of 24 spot interpretation of ANSI-colour
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| foreground | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | R = red | echo -e '\033[38;2;255;0;02m####\033[m' | R=255, G=0, B=0 || 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | G = green | echo -e '\033[38;2;;0;255;02m####\033[m' | R=0, G=255, B=0 || 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | B = blue | echo -e '\033[38;2;0;0;2552m####\033[m' | R=0, G=0, B=255 ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| background | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | R = red | echo -e '\033[48;2;255;0;02m####\033[m' | R=255, G=0, B=0 || 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | G = green | echo -e '\033[48;2;;0;255;02m####\033[m' | R=0, G=255, B=0 || 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | B = blue | echo -e '\033[48;2;0;0;2552m####\033[m' | R=0, G=0, B=255 ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
any surface-pictures
foreground Eight-spot abstract successful a .gif
inheritance Eight-spot abstract successful a .gif
colour abstract with their values
blinking
connected KDE-Terminal
colour-manner changeable
matter manner changeable
combining is Fine
much pictures
Ideas and Tips for Precocious Customers and Programmers:
Tin we usage these codes successful a programming communication?
Sure, you tin. I skilled successful bash, c, c++, d perl, python
Bash they dilatory behind the velocity of a programme?
I deliberation, Nary.
Tin we usage these connected Home windows?
Three/Four-spot Sure, if you compile the codification with gcc
any surface-pictures connected Victory-7
However to cipher the dimension of codification?
\033[
= 2, another components 1
Wherever tin we usage these codes?
Anyplace that has a tty
interpreterxterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
and truthful connected.
For illustration if you privation to colorize your output with mysql you tin usage Perl
#!/usr/bin/perl -nprint "\033[1m\033[31m$1\033[36m$2\033[32m$3\033[33m$4\033[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^\w])/g;
shop this codification successful a record sanction: pcc
(= Perl Colorize Quality) and past option the record a successful legitimate PATH
past usage it anyplace you similar.
ls | pcc
df | pcc
wrong mysql
archetypal registry it for pager
and past attempt:
[user2:db2] pager pccPAGER set to 'pcc'[user2:db2] select * from table-name;
It does NOT grip Unicode.
Bash these codes lone bash colorizing?
Nary, they tin bash a batch of absorbing issues. Attempt:
echo -e '\033[2K' # clear the screen and do not move the position
oregon:
echo -e '\033[2J\033[u' # clear the screen and reset the position
Location are a batch of freshmen that privation to broad the surface with system( "clear" )
truthful you tin usage this alternatively of system(3)
call
Are they disposable successful Unicode?
Sure. \u001b
Which interpretation of these colours is preferable?
It is casual to usage 3/4-bit
, however it is overmuch close and beauteous to usage 24-bit
.
If you bash not person education with html truthful present is a speedy tutorial:
24 bits means: 00000000
and 00000000
and 00000000
. All Eight-spot is for a circumstantial colour.1..8
is for and
9..16
for and
17..24
for
Truthful successful html #FF0000
means and present it is:
255;0;0
successful html #00FF00
means which present is:
0;255;0
Does that brand awareness? what colour you privation harvester it with these 3 Eight-spot values.
mention:
Wikipedia
ANSI flight sequences
tldp.org
tldp.org
misc.flogisoft.com
any blogs/net-pages that I bash not retrieve
The Linux bid formation is a almighty implement, and personalizing it tin enormously heighten your workflow. 1 communal customization is altering the output colour of the echo bid. By altering the colour, you tin detail crucial messages, differentiate betwixt varieties of output, and merely brand your terminal much visually interesting. This station volition usher you done assorted strategies to alteration the output colour of echo successful Linux, offering you with applicable examples and explanations.
Knowing However to Modify Echo Output Colour successful Linux
Altering the colour of echo output entails utilizing terminal flight sequences, particular quality combos that the terminal interprets arsenic instructions to modify matter attributes similar colour, kind (daring, italic, underline), and cursor assumption. These sequences usually commencement with an flight quality (\033 oregon \e) adopted by circumstantial codes. We'll research however to usage these sequences straight with echo and besides however to fit ahead aliases for much handy usage. Knowing these ideas volition empower you to customise your terminal output to your liking.
Utilizing ANSI Flight Codes Straight with Echo
ANSI flight codes are the about communal technique for including colour to your terminal output. These codes are embedded straight into the drawstring you're echoing. All codification consists of an flight series adopted by a numerical codification representing the colour oregon kind you privation to use. For illustration, the codification for reddish matter is \033[31m. To usage this with echo, you would see this series earlier the matter you privation to colour and past see a reset codification \033[0m last the matter to instrument to the default colour. This prevents each consequent output from being coloured.
echo -e "\033[31mThis text will be red.\033[0m" echo -e "\033[32mThis text will be green.\033[0m"
Present’s a breakdown of communal ANSI colour codes:
Colour | Codification |
---|---|
Achromatic | \033[30m |
Reddish | \033[31m |
Greenish | \033[32m |
Yellowish | \033[33m |
Bluish | \033[34m |
Magenta | \033[35m |
Cyan | \033[36m |
Achromatic | \033[37m |
Reset | \033[0m |
These codes tin besides beryllium mixed with another formatting choices similar daring, italic, and underline. For illustration, \033[1;31m volition food daring reddish matter. Experimenting with antithetic codes permits for a broad scope of customization.
Nevertheless bash I get a YouTube video thumbnail from the YouTube API?Creating Aliases for Coloured Echo Instructions
Typing retired the ANSI flight codes all clip you privation to colour your output tin beryllium tedious. A much businesslike attack is to make aliases successful your ammunition configuration record (e.g., .bashrc oregon .zshrc). An alias is a shortcut that replaces a bid with a longer, much analyzable bid. By creating aliases for coloured echo instructions, you tin simplify your workflow and trim the hazard of errors.
To make an alias, unfastened your ammunition configuration record successful a matter application:
nano ~/.bashrc
Adhd the pursuing strains to the record to make aliases for communal colours:
alias red='echo -e "\033[31m"' alias green='echo -e "\033[32m"' alias yellow='echo -e "\033[33m"' alias reset='echo -e "\033[0m"'
Last redeeming the record, you demand to origin it to use the modifications to your actual conference:
source ~/.bashrc
Present you tin usage these aliases to colour your output:
red "This text will be red." ; reset green "This text will be green." ; reset
This technique makes it overmuch simpler to adhd colour to your echo output and tin beryllium prolonged to see much analyzable formatting choices. See creating aliases for daring, italic, and underline matter arsenic fine.
Past basal colours, you tin besides specify inheritance colours and much precocious matter results. For illustration, \033[42m units the inheritance colour to greenish. Combining this with matter colours permits for equal larger customization. For a blanket database of ANSI flight codes, mention to outer sources similar Wikipedia's article connected ANSI flight codes.
"The powerfulness of the bid formation lies successful its flexibility. Customizing your terminal situation tin importantly better productiveness and brand your activity much pleasing."
Different utile method is to make features alternatively of aliases, which permits you to walk arguments to the colour instructions. For illustration:
color_echo() { color=$1 text=$2 case $color in red) color_code="\033[31m" ;; green) color_code="\033[32m" ;; yellow) color_code="\033[33m" ;; ) color_code="\033[0m" ;; esac echo -e "${color_code}${text}\033[0m" }
You tin past call this relation similar this:
color_echo red "This text will be red." color_echo green "This text will be green."
This presents a much structured and maintainable attack for analyzable coloring wants. Larn much astir bash scripting and features done sources similar the GNU Bash guide.
Successful decision, modifying the output colour of the echo bid successful Linux is a simple procedure that tin importantly heighten your bid-formation education. By knowing and using ANSI flight codes and creating aliases oregon features, you tin customise your terminal output to lawsuit your circumstantial wants. Experimentation with antithetic colours and types to discovery what plant champion for you and brand your bid formation situation much informative and visually interesting. Return vantage of these methods to better your productiveness and brand running successful the terminal a much pleasing education.
Tips and Secrets to Shell Efficiency
Tips and Secrets to Shell Efficiency from Youtube.com