a customized color for "echo" output [ SOLVED ]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ckonn
Level 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

a customized color for "echo" output [ SOLVED ]

Post by ckonn »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: customized color for "echo" output

Post by Habitual »

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 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: customized color for "echo" output

Post by xenopeek »

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: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: customized color for "echo" output

Post by austin.texas »

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 2018
Habitual

Re: customized color for "echo" output

Post by Habitual »

austin.texas wrote:The NC line is necessary.
No mystery in your life? :mrgreen:
Good catch!
ckonn
Level 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

Re: customized color for "echo" output

Post by ckonn »

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?
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: a customized color for "echo" output

Post by xenopeek »

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 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

Re: a customized color for "echo" output

Post by ckonn »

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!
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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 3
Level 3
Posts: 180
Joined: Wed Oct 01, 2014 7:03 pm

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

Post by ckonn »

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!
User avatar
zcot
Level 9
Level 9
Posts: 2832
Joined: Wed Oct 19, 2016 6:08 pm

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

Post by zcot »

Locked

Return to “Scripts & Bash”