Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
A regular text file is just that, a plain old simple text files with only alphanumeric characters.
If you would like to have certain words/lines formatted with different color attributes, you need to insert some control codes into the file.
I did this some time ago when I wanted to show my Pre-Login banner using different colors.
Here is the modified shell script with an example text file.
# This script displays a text file with color attributes for different
# The name of the text file to show is passed as the first argument
# to the script.
# ie: script.sh somefile.txt
# Copyright (C) 2013 Towheed Mohammed
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details at <http://www.gnu.org/licenses/>.
# These are the codes for the text attributes.
# [color_normal] Set the color attributes to normal.
# [bold_on] Set the bold attribute.
# [bold_off] Reset the bold attribute (normal intensity).
# [reverse_on] Set reverse video.
# [reverse_off] Reset reverse video.
# [black] Set text color to black.
# [red] Set text color to red.
# [green] Set text color to green.
# [brown] Set the text color to brown.
# [blue] Set the text color to blue.
# [magenta] Set the text color to magenta.
# [cyan] Set the text color to cyan.
# [white] Set the text color to white.
# [black_bkg] Set the background color to black.
# [red_bkg] Set the background color to red.
# [green_bkg] Set the background color to green.
# [brown_bkg] Set the background color to brown.
# [blue_bkg] Set the background color to blue.
# [magenta_bkg] Set the background color to magenta.
# [cyan_bkg] Set the background color to cyan.
# [white_bkg] Set the background color to white.
# Color attributes variable assignment.
# Foreground colors.
# Background colors
# Read the /etc/issue file.
echo -e "$(sed -e 's,\[color_normal],'"$normal"',g' \
-e 's,\[bold_on],'"$bold_on"',g' \
-e 's,\[bold_off],'"$bold_off"',g' \
-e 's,\[reverse_on],'"$reverse_on"',g' \
-e 's,\[reverse_off],'"$reverse_off"',g' \
-e 's,\[black],'"$black"',g' \
-e 's,\[red],'"$red"',g' \
-e 's,\[green],'"$green"',g' \
-e 's,\[brown],'"$brown"',g' \
-e 's,\[blue],'"$blue"',g' \
-e 's,\[magenta],'"$magenta"',g' \
-e 's,\[cyan],'"$cyan"',g' \
-e 's,\[white],'"$white"',g' \
-e 's,\[black_bkg],'"$black_bkg"',g' \
-e 's,\[red_bkg],'"$red_bkg"',g' \
-e 's,\[green_bkg],'"$green_bkg"',g' \
-e 's,\[brown_bkg],'"$brown_bkg"',g' \
-e 's,\[blue_bkg],'"$blue_bkg"',g' \
-e 's,\[magenta_bkg],'"$magenta_bkg"',g' \
-e 's,\[cyan_bkg],'"$cyan_bkg"',g' \
-e 's,\[white_bkg],'"$white_bkg"',g' < $1)"
Here is a sample text file:
[blue]This is blue text. [bold_on]This is blue text that's bold.[bold_off]
[white][green_bkg]This is white text on a green background.
[color_normal]This is the default colored text on the default background color.
[red]red text [brown]brown text [green]green text
I'm not sure of the printer supports the same text attributes as the shell. If it does, then it should print the same as it's displayed on the screen.
That's nice, but it doesn't help my scenario. I need to print the file, not merely display the file on the computer screen. When I print the output from your program on a real printer, it's all in black ink.
You seem to be missing the point. Again, a text file on its own doesn't have that ability. It's just a series of bytes that represent text characters in some character encoding. The only colors or formatting you could get would come from some other program interpreting the data contained and displaying it in an appropriate manner.
As an example, a web page is just a text file. Open up an html source file in a regular text browser and you'll see the raw text. But that text includes html mark-up codes, and if you open it in a browser window that mark up will be interpreted and formatted into a web page. That is, only a program with an html parser built in will be able to display it the way it's intended.
Colors in the console shell work the same way, except that the mark-up is in the form of ansi escape codes. Only a program that understands the escapes will be able to show you actual colors. Any other text viewer will just display the raw codes.
So your only option is to write up and store the text in a way that allows you to use colors. I recommend using something like libreoffice. Then you can either print it directly or save it as an .odf, .doc, or .pdf, any of which can hold color formatting and allow them to be displayed in their appropriate viewers. But it won't be a plain text file after that.