Search

Coloring the output of cat

The "cat" command is used to view the contents of a file with out opening it.
The cat displays the contents of the file in the same color as that of the terminal. This can be changed and cat can be made to display the text in a different color. This can be done using the command "tput".
We can chnage the terminal font using tput as shown below.



For e.g.



Now we will combine this with cat .
Let us say we have a file temp

temp:





Output:



We can put this in a script and allow the user to pass the number for the color as well as the file to be displayed on the command line.

cat_color:



Save it and give it execute permissions.



Execute it just like the cat command



Output:



To make this script to work like a command we can add the path to the script in the PATH as given in the post Making a script available in all folders or copy the script into /bin directory.

Follow by Email