a customized color for "echo" output [ SOLVED ]

Forum rules
Before you post please read how to get help
ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

a customized color for "echo" output [ SOLVED ]

Postby ckonn » Fri Apr 28, 2017 9:34 am

Hello,

I was wondering is it possible to implement some lines of code in a bash script so that every output from 'echo' command will appear in the terminal not in white, but in some different color? Light blue, for example.

thanks in advance
Last edited by ckonn on Mon May 01, 2017 10:13 am, edited 3 times in total.
linux mint 17.3 mate

Habitual
Level 13
Level 13
Posts: 4866
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: customized color for "echo" output

Postby Habitual » Fri Apr 28, 2017 10:19 am

It's 5 years old , but should still yield clues: http://stackoverflow.com/questions/5947 ... o-in-linux

quick sample in terminal to get you started :)

Code: Select all

RED='\033[0;31m'
echo -e "We ${RED}love${NC} LinuxMint"

User avatar
xenopeek
Level 24
Level 24
Posts: 21470
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: customized color for "echo" output

Postby xenopeek » Fri Apr 28, 2017 10:43 am

Here's my example. You can override the builtin echo function. Your echo function would call bash's builtin echo function to echo what you tell it to but prefix it with the sequence to set blue text color and postfix the sequence to reset the text color (so output of other commands isn't changed). I always put such sequences in variables to keep code readable.

Code: Select all

#!/usr/bin/env bash

readonly BLUE=$'\e[34m'
readonly RESET=$'\e[0m'

echo() {
        builtin echo "${BLUE}$@${RESET}"
}

echo "Hello blue world!"

For a number of reasons this isn't a very nice script. For one, it doesn't account for the times you may want to redirect the output of your program to some other program or save it to a file. You would want that done without color formatting. For another, it doesn't use nicer colors when your terminal supports 256 colors instead of just 16.

So, a slightly more involved example which disables using color formatting when the output isn't going to a terminal and which uses nicer (solarized) colors if your terminal supports 256 colors.

Code: Select all

#!/usr/bin/env bash

# Conditionally enable colors
if [[ -t 1 ]]; then
        if [[ $(tput colors) -eq 256 ]]; then
                # solarized colors if 256 color support
                readonly BLUE=$'\e[1;38;5;33m'
        else
                # fallback colors otherwise
                readonly BLUE=$'\e[34m'
        fi
        readonly RESET=$'\e[0m'
else
        readonly BLUE=''
        readonly RESET=''
fi

echo() {
        builtin echo "${BLUE}$@${RESET}"
}

echo "Hello blue world!"

To learn more about color sequences see http://misc.flogisoft.com/bash/tip_colo ... formatting.
Image

User avatar
austin.texas
Level 20
Level 20
Posts: 11708
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: customized color for "echo" output

Postby austin.texas » Fri Apr 28, 2017 11:36 am

Habitual wrote:

Code: Select all

RED='\033[0;31m'
echo -e "We ${RED}love${NC} LinuxMint"

The NC line is necessary.

Code: Select all

RED='\033[0;31m'
NC='\033[0m'
echo -e "We ${RED}love${NC} LinuxMint"
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2017

Habitual
Level 13
Level 13
Posts: 4866
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: customized color for "echo" output

Postby Habitual » Fri Apr 28, 2017 11:40 am

austin.texas wrote:The NC line is necessary.

No mystery in your life? :mrgreen:
Good catch!

ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

Re: customized color for "echo" output

Postby ckonn » Fri Apr 28, 2017 11:54 am

Habitual wrote:It's 5 years old , but should still yield clues: http://stackoverflow.com/questions/5947 ... o-in-linux

quick sample in terminal to get you started :)

Code: Select all

RED='\033[0;31m'
echo -e "We ${RED}love${NC} LinuxMint"


it works, but ... if I make the script like this:

#!/bin/bash

LightBlue='\033[1;36m'

echo -e "${LightBlue}Hello, world!"

date

this will set both, the output from 'echo' and the output from the 'date' command in light blue.

I want that only the output from the 'echo' command to be in light blue, and the output from the 'date' to stay in white.
How should I change the code?
linux mint 17.3 mate

User avatar
xenopeek
Level 24
Level 24
Posts: 21470
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: a customized color for "echo" output

Postby xenopeek » Fri Apr 28, 2017 12:25 pm

See my or austin.texas' example. You need to reset the colors after printing. In my example you just call echo and it handles that for you. In austin.texas' example the missing variable NC (no color, does the same as my RESET variable) from Habitual's example is defined and added at the end of the string to print with each echo that you want to use color.
Image

ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

Re: a customized color for "echo" output

Postby ckonn » Fri Apr 28, 2017 1:36 pm

so.

Code: Select all

#!/bin/bash
LightBlue='\033[1;36m'
echo -e "${LightBlue}Hello, world!" '\033[0m'
date

this solves the task!

as far as the proposal to override the file 'env' in /usr/bin/ I should say that I could't open it neither with nano nor with pluma or with some other program. more precisely I have open it with nano but it shows ... machine code, as I understand. and the nano shows also a message that the content of the file is decoded from Mac.

the file 'env" in my /usr/bin is a application/x-executable file.

and if you mean that 'env' is a sudirectory in /usr/bin/ and it contains some file called 'bash' ...
by the command cd /usr/bin/env in the terminal I receive an error message: bash: cd: /usr/bin/env: is not a directory

nevertheless, thank you all for the help!
linux mint 17.3 mate

User avatar
xenopeek
Level 24
Level 24
Posts: 21470
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: a customized color for "echo" output [ S O L V E D ]

Postby xenopeek » Fri Apr 28, 2017 2:01 pm

Eh? You're not overriding /usr/bin/env. The line #!/usr/bin/env bash is just the first line of your script that says to "use the bash command as found in your path to run this script".

You're overriding the echo function in your own script as per my example. You define the echo function to add the blue color to the output. You put the rest of your script after it so any time in your script you call echo it calls your function which adds blue to the output.
Image

ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

Re: a customized color for "echo" output [ S O L V E D ]

Postby ckonn » Sat Apr 29, 2017 10:40 am

xenopeek wrote:Eh? You're not overriding /usr/bin/env. The line #!/usr/bin/env bash is just the first line of your script that says to "use the bash command as found in your path to run this script".

You're overriding the echo function in your own script as per my example. You define the echo function to add the blue color to the output. You put the rest of your script after it so any time in your script you call echo it calls your function which adds blue to the output.


Oh, I see! I have added the second variant of code to my bash scripts, and it works great!

thanks for the help!
linux mint 17.3 mate

zcot
Level 1
Level 1
Posts: 25
Joined: Wed Oct 19, 2016 6:08 pm

Re: a customized color for "echo" output [ SOLVED ]

Postby zcot » Mon May 01, 2017 11:14 am



Return to “Scripts & Bash”