The Computer Oracle

Different Color for command and output

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping

--

Chapters
00:00 Different Color For Command And Output
00:23 Answer 1 Score 7
00:43 Accepted Answer Score 6
02:35 Answer 3 Score 4
03:03 Answer 4 Score 1
03:33 Answer 5 Score 1
04:49 Thank you

--

Full question
https://superuser.com/questions/610058/d...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #terminal #colors

#avk47



ANSWER 1

Score 7


You can pass the output to a program which does the colouring. For example, there are programs like pycolor:

pycolor module.py

To build your own:

red="$(tput setaf 1)"
reset="$(tput sgr0)"
echo $'foo\nbar' | sed -e "s/\(bar\)/$red\1$reset/"

More on colour output.




ACCEPTED ANSWER

Score 6


Not really; the color of a given program's output is controlled by the command, not by the terminal.

That said, assuming your terminal understands ANSI escape codes for color (most do), you could use escape codes to set your desired prompt color at the beginning of the prompt, and your desired output color at the end; this would result in your command lines also being in whatever color you set for output, but that's probably about as close as you're going to get to what you're looking for. For example, the following:

$ export PS1='\[\033[00;35m\]\u\[\033[00m\]@\[\033[00;35m\]\H\[\033[00m\]:\[\033[00;33m\]\W\[\033[00m\] \$\[\033[00;34m '

will give you a prompt that looks like:

user@host:wd $ _

with 'user' and 'host' in purple, 'wd' (your cwd) in brown (dark yellow), and everything after the '$ ' in whatever your terminal uses for light blue. (A default xterm will render this in cyan; gnome-terminal seems to default to a rather nice shade of cornflower blue.)

The actual color code, as specified in the necessary format for bash, is, e.g., \[\033[00;35m\], where 00;35 is the actual color specification; the leading 00 rarely changes, but can produce IIRC bold (01), underline (??), and reverse video (??) effects, while the trailing '35' is the actual color, which in this case is purple.

It's surprisingly hard to find a decent list of ANSI color codes, but foreground colors run from 30 through 37, and background ones from 40 through 47, as follows:

color        fg  bg
black        30  40
red          31  41
green        32  42
yellow       33  43
blue         34  44
purple       35  45
cyan         36  46
gray/white   37  47

Do keep in mind that, since you're setting a default color for everything that follows your prompt, programs you run which don't set their own colors via escape codes are going to take that color -- for example, if you run a pager, you're likely to see its output in the same color you've set for other commands. Meanwhile, commands which do set their own colors via escape codes will ignore your efforts entirely.




ANSWER 3

Score 4


Yes I have this working for bash, on linux and mac OS at least.

First, set some attribute variables, and the PS1 prompt to leave the terminal set with the desired command-line attributes, for example:

bold="\[$(tput bold)\]"
none="$(tput sgr0)"
export PS1="\$ $bold"

Then, use the bash DEBUG trap, which gets executed just before every command, to change text attributes for command output:

trap 'echo -ne "${none}"' DEBUG



ANSWER 4

Score 1


Another alternative (on a command by command basis) is grc - generic colorizer. It takes a bit of work to setup the regular expressions that it uses to colorize things. I found out about it here:

https://unix.stackexchange.com/questions/8414/how-to-have-tail-f-show-colored-output?newsletter=1&nlcode=17737|321c

and here:

https://unix.stackexchange.com/questions/26313/colored-find-output

I'm not a flash with regexes, so I haven't worked with these commands much.




ANSWER 5

Score 1


Throw these into your .bashrc file and then source the file after saving.

blk=$'\x1b[90m' # Sets text to black
red=$'\x1b[31m' # Sets text to red
grn=$'\x1b[92m' # Sets text to green
ylw=$'\x1b[93m' # Sets text to yellow
blu=$'\x1b[94m' # Sets text to blue
pur=$'\x1b[95m' # Sets text to purple
cyn=$'\x1b[96m' # Sets text to cyan
wht=$'\x1b[97m' # Sets text to white
rst=$'\x1b[0m'  # resets to default terminal color

Once this is done, you can then use any of these in your terminal permanently or until you either comment the variables out or delete them completely. You can use them in scripts on your machine or with command substitution or whatever. Here is a following example of how I use them personally:

echo -e "${red}This text is red.${rst}"
printf "${grn}This text is green.\n${rst"

I usually use these with individual echo and/or printf commands but you can use them to colorize other commands as well such as the ping command. Add the following to .bashrc to get the effects. Then source your .bashrc file and run the cping command to use ping with purple text. You can set each command function to use any color your terminal supports.

# Add this to your .bashrc file
colored_ping() {

    echo -e "${pur}$(ping $1)${rst}"
}

alias cping='colored_ping'

Then try:

user@linux:~# cping superuser.com

Your output for cping will be purple. If you want to use multiple colors in the same output you can add them in with unique ways. How you do it is completely up to you. For example:

echo -e "${red}This is red and ${blu}this is blue and ${grn}this is green.${rst}