Color Linux command output

后端 未结 10 1339
天命终不由人
天命终不由人 2020-12-09 19:03

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 li

相关标签:
10条回答
  • 2020-12-09 19:27

    You can use escape sequences to change the font color of any output to the bash shell. Here are some of the color codes you'll need:

    BLACK="\033[30m"
    RED="\033[31m"
    GREEN="\033[32m"
    YELLOW="\033[33m"
    BLUE="\033[34m"
    PINK="\033[35m"
    CYAN="\033[36m"
    WHITE="\033[37m"
    NORMAL="\033[0;39m"
    

    Once these are defined, you can use them in normal echo commands. For instance:

    echo -e $GREEN this text is green $NORMAL and this is normal
    

    Note that the -e is not always necessary, but on some OSs (incl. osx) is required for to enable escape sequences.

    Given these definitions you can build scripts and pipes to color the output from other commands. Here is a complete example I use to color the output from svn up:

    #!/bin/bash
    
    BLACK="\033[30m"
    RED="\033[31m"
    GREEN="\033[32m"
    YELLOW="\033[33m"
    BLUE="\033[34m"
    PINK="\033[35m"
    CYAN="\033[36m"
    WHITE="\033[37m"
    NORMAL="\033[0;39m"
    
    TMPFILE=.cvsup.tmp
    
    svn up > $TMPFILE
    svn status >> $TMPFILE
    printf $YELLOW
    grep -e ^"\? " -e ^"I " $TMPFILE
    printf $GREEN
    grep -e ^"R " -e ^"U " -e ^"G " $TMPFILE
    printf $BLUE
    grep -e ^"M " -e ^"E " $TMPFILE
    printf $RED
    grep -e ^"C " -e ^"! " -e ^"X " -e ^"~ " $TMPFILE
    printf $PINK
    grep ^"R " $TMPFILE
    printf $PINK
    grep ^"D " $TMPFILE
    printf $CYAN
    grep ^"A " $TMPFILE
    
    printf $NORMAL
    rm $TMPFILE
    

    You can also look at tput.

    0 讨论(0)
  • 2020-12-09 19:27
    norm="$(printf '\033[0m')" #returns to "normal"
    bold="$(printf '\033[0;1m')" #set bold
    red="$(printf '\033[0;31m')" #set red
    boldred="$(printf '\033[0;1;31m')" #set bold, and set red.
    
    somecommand | sed -e "s/someregexp/${boldred}&${norm}/g"  # will color any occurence of someregexp in Bold red
    
    printf "%s" "$red" ; locate something ; printf "%s" "$norm"  # will color output of locate something in red
       #I (ab)use printf "%s" "something", as it's more portable than echo,and easy to modify
    

    There are many other ways (create a function/script that can colorize a regexp, for example, and then : somecommand | colorize -c green 'foo.*bar' 'other' )

    0 讨论(0)
  • 2020-12-09 19:32

    The main tool for that is of course lolcat!

    locate -bir pdf | lolcat
    

    To install:

    sudo apt install lolcat 
    

    See man lolcat for customizations.

    0 讨论(0)
  • 2020-12-09 19:35

    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

    0 讨论(0)
  • 2020-12-09 19:36

    You should have a look at the hl command available on git hub:

    git clone http://github.com/mbornet-hl/hl
    

    and on :

    http://www.flashnux.com/notes/page_000022_US.html

    hl is a Linux command written in C, especially designed to color a text file or the output of a command. You can use up to 42 colors simultaneously, and use a configuration file to simplify command lines. You can colorize the output of every command that can be piped to another one. And if you know what regular expressions are, it will be very easy for you to use. You can use the man page to understand how to use it.

    0 讨论(0)
  • 2020-12-09 19:40

    Use the tput command.

    Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256). Using the setaf and setab capabilities, we can set the foreground and background colors. The exact rendering of colors is a little hard to predict. Many desktop managers impose "system colors" on terminal windows, thereby modifying foreground and background colors from the standard. Despite this, here are what the colors should be:

    Value Color

    0 Black

    1 Red

    2 Green

    3 Yellow

    4 Blue

    5 Magenta

    6 Cyan

    7 White

    8 Not used

    9 Reset to default color

    Actual example: set color to red, cat and then change color back:

    tput setaf 1; cat /proc/meminfo ; tput setaf 9
    
    0 讨论(0)
提交回复
热议问题