Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color Linux command output [duplicate]

Tags:

linux

bash

For example, I'd like to color the output of the locate command so it's easily distinguished from the other terminal text.

It should work something like this:

locate -bir pdf | some_command_to_color_the_result

Coloring shouldn't be limited for the locate command only: I need a generic solution that colors text using pipelines, for example feeding it the output of grep or cat.

If there's no built-in Linux command, any hints on how to create one are appreciated.

like image 786
user3158243 Avatar asked Jan 03 '14 18:01

user3158243


People also ask

Can colorized output be captured via shell redirect?

Redirecting doesn't strip colors, but many commands will detect when they are sending output to a terminal, and will not produce colors by default if not.

How do you use color commands in Linux?

A script can use escape sequences to produce colored text on the terminal. Colors for text are represented by color codes, including, reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37.

How do you repeat a command in Linux?

First, the easiest way to repeat a command is simply by typing !!. If you were logged into a Linux server and waiting for a coworker to log in, for example, you might want to repeat the who command shown below until you see your coworker's username. Typing !! after the initial who command will do this for you.


1 Answers

The following answered my question:

1- I create an alias in my .bashrc

alias color='grep --color .'

2- Then whenever I want to color the pipeline text output I use color alias like:

locate -bir pdf | color

This will color the output to red

like image 71
user3158243 Avatar answered Oct 14 '22 08:10

user3158243