a customized color for "echo" output [ SOLVED ]
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
a customized color for "echo" output [ SOLVED ]
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
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: customized color for "echo" output
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
quick sample in terminal to get you started
Code: Select all
RED='\033[0;31m'
echo -e "We ${RED}love${NC} LinuxMint"
Re: customized color for "echo" output
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.
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.
To learn more about color sequences see http://misc.flogisoft.com/bash/tip_colo ... formatting.
Code: Select all
#!/usr/bin/env bash
readonly BLUE=$'\e[34m'
readonly RESET=$'\e[0m'
echo() {
builtin echo "${BLUE}$@${RESET}"
}
echo "Hello blue world!"
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!"
- austin.texas
- Level 20
- Posts: 12003
- Joined: Tue Nov 17, 2009 3:57 pm
- Location: at /home
Re: customized color for "echo" output
The NC line is necessary.Habitual wrote:Code: Select all
RED='\033[0;31m' echo -e "We ${RED}love${NC} LinuxMint"
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
Linux Linx 2018
Re: customized color for "echo" output
No mystery in your life?austin.texas wrote:The NC line is necessary.
Good catch!
Re: customized color for "echo" output
it works, but ... if I make the script like this: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 startedCode: Select all
RED='\033[0;31m' echo -e "We ${RED}love${NC} LinuxMint"
#!/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?
Re: a customized color for "echo" output
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.
Re: a customized color for "echo" output
so.
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!
Code: Select all
#!/bin/bash
LightBlue='\033[1;36m'
echo -e "${LightBlue}Hello, world!" '\033[0m'
date
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!
Re: a customized color for "echo" output [ S O L V E D ]
Eh? You're not overriding /usr/bin/env. The line
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.
#!/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.
Re: a customized color for "echo" output [ S O L V E D ]
Oh, I see! I have added the second variant of code to my bash scripts, and it works great!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.
thanks for the help!
Re: a customized color for "echo" output [ SOLVED ]
to expand the knowledge:
https://www.google.com/#q=bash+color+codes
https://www.google.com/#q=bash+color+codes