Sign in Welcome! Log into your account your username your password Forgot your password? Get help Password recovery Recover your password your email A password will be e-mailed to you. HomeProgrammingTips on how to Change the Output Shade of Echo in Linux Programming Tips on how to Change the Output Shade of Echo in Linux By Admin October 28, 2022 0 1 Share FacebookTwitterPinterestWhatsApp Introduction The echo command outputs a given string to the commonplace output pipe, sometimes pointing to the terminal. Though the usual output pipe can level to different interfaces – the echo command is often used to print and show messages within the terminal. By default, the textual content of the displayed messaged inherits the colour of different textual content within the terminal (which is customizable in and of itself). Nonetheless – there are a number of methods you may alter the output coloration of echo – each for particular person strings and for whole messages. On this quick information, we’ll check out how one can change the output coloration of echo in Linux-based programs, utilizing ANSI escape codes, tput and how one can make this course of much less verbose in Bash scripts. Change Output Shade with ANSI Escape Codes The simplest method to change the colour is thru ANSI escape sequences/codes. All ANSI escape codes begin with the Escape character, which may be represented in varied codecs – 27 in decimal, x1B in hexadecimal, because the management key ^[, or �33 in octal format. The sequences are then followed by the command: �33[command Where the opening bracket (Control Sequence Introducer) is optional, but helps separate the command from the escape character. When you put a color code as the command, it changes the color of the oncoming text: �33[0;34 0;34 is the code for the color blue, for example. With this alone, you can change the color of text in echo with: �33[0;34Text Where Text would be colored blue. Alternatively, consider a simple bash script to print “Welcome to France” in the colors of the French flag: #!/bin/bash BLUE='�33[0;34m' WHITE= '�33[0;37m' RED= '�33[0;31m' echo -e "${Blue}Welcome ${WHITE}to ${RED}France" The optional -e flag of the echo command allows you to use special characters like n (newline) and t (tab) inside of the input string. Once you run the script: $ ./colors.sh It results in: The ANSI codes aren’t limited to color – but can also be applied for style. The codes 0..9 represent text styles, while the codes 30...37 represent colors: Color Codes Text Styles Codes Black 30 Simple text 0 Red 31 Bold text 1 Green 32 Low intensity text 2 Brown/Orange 33 Underline text 4 Blue 34 Blinking text 5 Purple 35 Invisible text 8 Cyan 36 Strikethrough text 9 Light Gray 37 Let’s create a bash script to explore some of these options: #!/bin/bash echo -e "33[0;33mSample text" echo -e "33[1;33mBold text" echo -e "33[2;33mLow intensity text" echo -e "33[4;33mUnderline text" echo -e "33[5;33mBlinking text" echo -e "33[8;33mInvisible text" echo -e "33[9;33mStrikethrough text" Running this script results in: Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! Similarly, you can change the background color of these texts using codes 40..47: color Codes Black 40 Red 41 Green 42 Brown/Orange 43 Blue 44 Purple 45 Cyan 46 Light Gray 47 Changing the background of a given string boils down to the same rule as when changing the font color – the code itself changes the behavior: #!/bin/bash BLUE='33[0;44m' BLACK='33[0;30m' WHITE='33[0;30;47m' RED='33[0;41m' echo -e "${BLUE}Welcome ${WHITE}to ${RED}France" Note: As a rule of thumb – you can translate font colors to background colors by adding 10. 30 is black font color, 40 is black background color. Change the Output Color with the tput An alternative to ANSI codes is using the tput command: $ tput setaf color_code setf allows for 8 colors, while setaf allows for 256 colors so depending on the command you’re using, you can go between 0..7 and 0..255 as the color codes. Both commands dedicate 0..7 to the same color codes, while with setaf, 8..15 are high-intensity colors, and 16..231 are different hues of the first 8, and 232..255 are greyscale colors: Credit: Wikipedia Finally, tput also allows you to change the background color, add bold, lower the intensity, etc. with other commands: Text Styles Commands Foreground color setaf Background color setab No style sgv0 Bold text bold Low-intensity text dim Underline text smul Blinking text blink Reverse text rev Let’s create another script that uses tput to change the output color echo: #!/bin/bash YELLOW=`tput setaf 3` echo "${YELLOW}Changing" WHITE=`tput setaf 7 bold` echo "${WHITE}Colors" BLUE=`tput setaf 4 smul` echo "${BLUE}With tput" CYAN=`tput setaf 6 blink` echo "${CYAN}is less" RED=`tput setab 1 setaf 7` echo "${RED}verbose!" This script will print the following output in the terminal: The tput command delivers an easy way to print any color through a simple color code. Let’s now create a script that can print every color code available for the tput command: #!/bin/bash tput init end = $ (($ (tput colors) - 1)) w = 1 for c in $ (seq 0 $end) do eval "$(printf " tput setaf % 3 s " " $c ")" echo - n "$_" [[$c - ge $ ((w * 2))]] offset = 2 || offset = 0 [[$ (((c + offset) % (w - offset))) - eq $ (((w - offset) - 1))]] echo executed tput init It’s going to print 0 to 255 coloration and their codes: Conclusion On this quick information – we have taken a take a look at how one can change the colour of echo‘s output in Linux – utilizing ANSI Escape Sequences and the tput command. We have additionally explored how one can make the method much less verbose utilizing Bash variables, tweak background colours and elegance textual content. Share FacebookTwitterPinterestWhatsApp Previous articleConstructing a TypeScript CLI with Node.js and CommanderNext articleThis is a primary have a look at Age of Empires 2: Definitive Version on console Adminhttps://www.handla.it RELATED ARTICLES Programming New Options at raywenderlich.com — September 2022 Replace October 28, 2022 Programming Widget Testing With Flutter: Getting Began October 28, 2022 Programming The Overflow #149: Stack Overflow with out the web October 28, 2022 LEAVE A REPLY Cancel reply Comment: Please enter your comment! Name:* Please enter your name here Email:* You have entered an incorrect email address! Please enter your email address here Website: Save my name, email, and website in this browser for the next time I comment. - Advertisment - Most Popular 10 Examples to Grasp ggplot2: Line plots | by Soner Yıldırım | Oct, 2022 October 29, 2022 Digital and analog design flows obtain TSMC certification October 29, 2022 java – Handle sport states of a card sport with MVC October 29, 2022 Raspberry Robin Operators Promoting Cybercriminals Entry to Hundreds of Endpoints October 29, 2022 Load more Recent Comments