Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Problem with text coloring [SOLVED].

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
Mad_7
Posts: 111
Joined: 2010-01-09 21:05
Location: Patra, Greece, Europe.

Problem with text coloring [SOLVED].

#1 Post by Mad_7 »

Hi! :D

I'm on Bash and I want to display, some colored text onscreen.
I gave

Code: Select all

CYAN='\033[0;36m'
and then

Code: Select all

LPURPLE='\033[1;35m'
(which can be permanent adding them to .bashrc).
(First is for Cyan and later for the Light Purple ASCII colors).

Then giving:

Code: Select all

echo -e "${CYAN}This is a ${LPURPLE}nice${CYAN}, colored text."
, outputs the "This is a nice, colored text." string, all words in cyan, except "nice" which is in light purple.
So far, so good! :D

The problem is, that giving the above command from inside a script file, outputs the same string, but without any coloring. All characters are white.
Any thoughts? :cry:

Of course one can achieve the desired (colored) output, with the help of a computer language. (I did it with FreeBASIC, but naturally any language will do).
I'm just curious why the above mentioned command didn't work.
PS. I also tried with printf (instead of echo), with similar results.
TIA! :wink:
M.
Last edited by Mad_7 on 2018-02-06 23:47, edited 1 time in total.
For the glorious men, every land is a grave.
Pericles' Funeral Oration. Thucydides.

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Problem with text coloring.

#2 Post by bw123 »

resigned by AI ChatGPT

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Problem with text coloring.

#3 Post by Head_on_a_Stick »

Mad_7 wrote:giving the above command from inside a script file, outputs the same string, but without any coloring
You should post the actual script rather than a vague description.

I suspect that your script looks something like this:

Code: Select all

#!/bin/bash
echo -e "${CYAN}This is a ${LPURPLE}nice${CYAN}, colored text."
This will not work because the script does not read ~/.bashrc and so has no idea what $CYAN & $LPURPLE are.

Try this instead:

Code: Select all

#!/bin/sh
CYAN='\033[0;36m'
LPURPLE='\033[1;35m'
echo -e "${CYAN}This is a ${LPURPLE}nice${CYAN}, colored text."
Or perhaps:

Code: Select all

#/bin/bash
. ~/.bashrc
echo -e "${CYAN}This is a ${LPURPLE}nice${CYAN}, colored text."
^ That may have unwanted side-effects though.

@bw123, as you can see, dash works just fine for this ;)
deadbang

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Problem with text coloring.

#4 Post by bw123 »

Head_on_a_Stick wrote: @bw123, as you can see, dash works just fine for this ;)
see the faq, also http://www.tldp.org/LDP/abs/html/colorizing.html

ANSI escape sequences are emphatically non-portable.
There also may be differences between 'echo' builtin and /bin/echo that could add to the conversation. The link to the dash/bash-differences is something I always use when recommending the bash faq.
resigned by AI ChatGPT

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Problem with text coloring.

#5 Post by debiman »

if ncurses is installed, i recommend using e.g.

Code: Select all

red="$(tput setaf 1)"
inside the script.

User avatar
Mad_7
Posts: 111
Joined: 2010-01-09 21:05
Location: Patra, Greece, Europe.

Re: Problem with text coloring.

#6 Post by Mad_7 »

MANY THANKS guys!!! :D
That was awesome!

My mistake was that I didn't take in account, that environment variables needs to be stated in the script.

My updated script is:

Code: Select all

CYAN='\033[0;36m'
LPURPLE='\033[1;35m'
echo -e "\n\n${CYAN}This is a ${LPURPLE}nice${CYAN}, colored text.\n\n\a"
Cyan and Light Purple is stated and also added a couple of line feeds (\n) before and after the text and also a bell ring (\a).
I think,ncurses should be installed (otherwise, the letters are white) and console's beep should be enabled (otherwise, no sound alarm).

I have to read a lot before actually getting comfortable with it, but what concerns me is the lack of portability between different distributions, even if both using Bash.
The tldp guide, clearly discourage the use of color, but coloring the output text is a sure way to get user's attention.
Also the audible alarm (bell ring), is very important in the case of a hidden terminal.

From what I read, seems like bash scripting is an easy way to "get things glued together", without the need of a high-level computer language (BASIC, C, Paschal etc).
Maybe it's only my impression, but with high-level languages, you can do the job done with less effort and in a more portable way (though it's maybe not the case for complex scripts).
What's your take?
Bash scripting or computer language?
(In terms of easier syntax and more portability).
For the glorious men, every land is a grave.
Pericles' Funeral Oration. Thucydides.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Problem with text coloring.

#7 Post by debiman »

Mad_7 wrote:What's your take?
Bash scripting or computer language?
my opinion is heavily influenced by my abilities: i know my way around bash, so that's what i use.

also depends on the use case; for some tasks, it's better to write in C & compile (because bash would run too hot), for others it's pointless to even write a shell script.

PS: i don't think you need ncurses to have colors in your terminal.

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Problem with text coloring.

#8 Post by bw123 »


what concerns me is the lack of portability between different distributions, even if both using Bash.
That concerns me too, but it's not just the portability that I think about.
There seem to be different rules for different terminal emulators, and different shells on the -same- distribution.
I think we can't be sure that what works today will always work unless we try to conform to POSIX. That's why I run all my scripts on dash.

It's a complicated subject. I like bash, it's the login shell, but dash is the default /bin/sh and that confuses things. At least for me...
resigned by AI ChatGPT

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: Problem with text coloring [SOLVED].

#9 Post by HarborView »

Okay, but this is an excerpt of how I do it:

Code: Select all

E=$(printf "\e")  # Escape character ASCII 27
cyn=$E'[0;36m' # Dark cyan
Cyn=$E'[1;36m' # Bright cyan
cyN=$E'[0;46m' # White on cyan background
CyN=$E'[1;46m' # Bright white on cyan background
RST=$E'[0m'  # Reset to white on black.

echo "You can go with "$cyn" THIS $RST or you can go with "$Cyn" THAT "$RST
echo "You can go with "$cyN" THIS $RST or you can go with "$CyN" THAT "$RST
Then I don't have to remember echo -e . The colors are more easily available.

Post Reply