I'm trying to make a "Cryptocurrency news" script using curl and so far it works, but not completely the way I intended.

Here is the code for the main part of the script:

curl -s https://www.coindesk.com/ \
  | grep '<h3>\|datetime\|<p>' \
  | sed "s/<h3>//;" \
  | sed 's/timeauthor">//;' \
  | cut -d '>' -f2 | cut -d '<' -f1 \
  | head -46 \
  | tail -39 \
  | sed '3G;6G;9G;12G;15G;18G;21G;24G;27G;30G;33G;36G;39G'

I'm trying to add color to the lines containing the headline and the time, so it'll be easier to read. This has proven difficult.

First I created color variables, like this:

n=`echo -en "\e[0m"`
b=`echo -en "\e[1m"`
r=`echo -en "\e[31m"`
a=`echo -en "\e[36m"`

I've tried to add them to the script a couple of ways, but so far nothing has worked out.

Here is one example:

sed "s/<h3>/`echo "${r}${b}`/;" | sed 's/timeauthor">/`echo "${a}`/;'

I can't even get the color to show. Another problem is, even if I could, I'm not really sure how to add the ${n} variable to get the text back to normal again...

The entire "working" version of the script:


# Color variables
 n=`echo -en "\e[0m"`
 b=`echo -en "\e[1m"`
 r=`echo -en "\e[31m"`
 a=`echo -en "\e[36m"`

# Prints news from coindesk.com
curl -s https://www.coindesk.com/ | grep '<h3>\|datetime\|<p>' | sed "s/<h3>//;" | sed 's/timeauthor">//;' | cut -d '>' -f2 | cut -d '<' -f1 | head -46 | tail -39 | sed '3G;6G;9G;12G;15G;18G;21G;24G;27G;30G;33G;36G;39G'

I'm fairly new to Linux and started bash scripting one week ago today, so this might not even be the best solution to accomplish what I'm looking to do. Even the working parts seem "overly complicated" and a bit hacked together. Especially the head/tail parts, but that was the best I could come up with.

  • 1
    I would suggest using an rss feed for this instead if possible, Feb 6, 2018 at 12:23

1 Answer 1


Short-and-simple answer

Use tput setaf $COLOUR_CODE to generate coloured output. Replace $COLOUR_CODE with an integer between 1 and 8, pertaining to 8 basic colours (mapped to the sequences \e[31m to \e[38m respectively.

So in the case of your script:

# Color variables
n=`tput sgr0`
b=`tput bold`
r=`tput setaf 1`
a=`tput setaf 6`

More detailed explanations

First of all, never use echo for outputting escape sequences like "\e[31m". The command is a portability train-wreck, and its -n -e switches aren't universally supported. Use printf instead:

printf '\e[31m'

This goes for other C-style escapes like \n and \t as well:

# Using Bash (Linux)
$ echo "foo\nbar"

# Using Ksh (*BSD)
$ echo "foo\nbar"

Secondly, hard-coding ANSI escape codes isn't the best way to generate coloured output. The tput(1) command exists for this purpose:

tput setaf 2  # Generates green text
tput bold     # Generate bold text
tput sgr0     # Reset those fancy effects

# To use them as variables:
bold=$(tput bold)
green=$(tput setaf 2)
reset=$(tput sgr0)
echo "${bold}HEADING${reset}"

Third, it's important to understand that coloured terminal output isn't universal between systems, terminal emulators, or hardware. tput mightn't even be available in some circumstances, and relying on highlighting to make TTY text readable is bad practice.

  • I don't believe this provides an answer to the question. Please modify your answer to show OP how he can add colors to his output.
    – jesse_b
    Feb 6, 2018 at 12:34
  • Maybe I'm not being clear enough. In order to answer ops question you will need to actually complete his code in order to make it work. As of now you are simply explaining how tput works. Which, I agree tput is better suited for ops needs but that is a comment and not an answer.
    – jesse_b
    Feb 6, 2018 at 12:46
  • @Jesse_b Ugh. I'm gonna leave this for somebody more familiar with conduct regulations and go back to lurking and voting on answers.
    – user101379
    Feb 6, 2018 at 13:09
  • Here are some guidelines on answering questions if you are confused: unix.stackexchange.com/help/how-to-answer
    – jesse_b
    Feb 6, 2018 at 13:11
  • I'm still not sure how to integrate it in the code I have so far.
    – discipulus
    Feb 6, 2018 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .