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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With