Ubuntu – Display part of fortune in different color

bashrccommand linefortune

I created a custom fortune file for my favourite tweets in the format tweet and then the @username in a new line. An Example is:

"You've changed."
"Yeah I know, I'm a Transformer."

@LetsQuoteComedy

I want to display the tweet in a different color, say white, and the @username in another color, like black. How can I do this in the terminal?

Also, I would like to put that in my .bashrc file.

Best Answer

Source: coloring command output

The code:

#!/bin/bash
OIFS=$IFS #save old IFS

frtn=$(fortune tweets) #load up a fortune
IFS='@' #new IFS
arr=($frtn) #split fortune into array along IFS

tweet=$(tput setaf 3)"${arr[0]}"$(tput setaf default) #tweet - dark yellow color
uname=$(tput setaf 7)"@${arr[1]}\n"$(tput setaf default) #create @username - white
echo -e "$tweet$uname"; #to retain newlines in original fortune, echo between quotes

IFS=$OIFS #restore old IFS

Just copy paste that into your .bashrc file.

EDIT

The result: enter image description here

Related Question