Color Linux command output

后端 未结 10 1362
天命终不由人
天命终不由人 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

    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' )

提交回复
热议问题