Problem with text coloring [SOLVED].

Need help with C, C++, perl, python, etc?

Problem with text coloring [SOLVED].

Postby Mad_7 » 2018-02-05 23:18

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
Mad_7
 
Posts: 111
Joined: 2010-01-09 21:05
Location: Patra, Greece, Europe.

Re: Problem with text coloring.

Postby bw123 » 2018-02-06 14:31

User avatar
bw123
 
Posts: 3135
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Problem with text coloring.

Postby Head_on_a_Stick » 2018-02-06 16:50

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 ;)
Charlie don't hack
User avatar
Head_on_a_Stick
 
Posts: 7635
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Problem with text coloring.

Postby bw123 » 2018-02-06 17:08

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.
User avatar
bw123
 
Posts: 3135
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Problem with text coloring.

Postby debiman » 2018-02-06 18:09

if ncurses is installed, i recommend using e.g.
Code: Select all
red="$(tput setaf 1)"
inside the script.
User avatar
debiman
 
Posts: 2499
Joined: 2013-03-12 07:18

Re: Problem with text coloring.

Postby Mad_7 » 2018-02-06 23:47

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
Mad_7
 
Posts: 111
Joined: 2010-01-09 21:05
Location: Patra, Greece, Europe.

Re: Problem with text coloring.

Postby debiman » 2018-02-07 17:46

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
debiman
 
Posts: 2499
Joined: 2013-03-12 07:18

Re: Problem with text coloring.

Postby bw123 » 2018-02-07 18:03


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...
User avatar
bw123
 
Posts: 3135
Joined: 2011-05-09 06:02
Location: TN_USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable