-1

I've set the TERM environment variable like so

export TERM=linux-m

to switch to non-color term. Now when i issue ls -l i indeed got no color output, but when i use for e.g: grep --color abc 1.txt it still returns colored output? Why doeas setting the variable affects ls command and not grep?

5
  • Did you try to explicitly tell ls to color its output?
    – Seth
    Jan 26, 2018 at 11:47
  • Yes when i use ls --color=always i also get no colors with ls. BTW why does it returns colors when I've set monochrome terminal?
    – Ringger81
    Jan 26, 2018 at 12:04
  • I get color even with ls --color=always or ls --color=auto
    – vera
    Jan 26, 2018 at 12:25
  • @Seth Can you just answer my last question?
    – Ringger81
    Jan 28, 2018 at 18:00
  • @Ringger81 no, I'm not certain why it does either. It was just something to try. if it did work with always it might've been that it's "enforcing" the monochrome ... well as it is, it apparently isn't. It's likely outputting the ANSI escape codes for colors. That would be my guess.
    – Seth
    Jan 29, 2018 at 6:42

1 Answer 1

0

from ls man page:

With --color=auto, ls emits color codes only when standard output is connected to a terminal.

Consequently, if --color is set to :

  • always : ls emits color even the output is redirected to a file ;
  • auto : ls emits color's code if the output is a terminal, eg. it is not redirected to a file or a pipe, even if terminal has no coloring, as it is not explicitly said that it depends on the terminal capabilities.

Considering grep, I assume that the behavior is the same (try grep --color=auto... | less and grep --color=always ... | less and you will see the results)

6
  • Ok but my question was how can grep return colored results if the terminal is indeed monochrome (linux-m)?
    – Ringger81
    Jan 28, 2018 at 18:20
  • oh sorry I didnt meant that. If the underlying terminal emulator (very old xterm, ...) really does not have color capabilities, there is no mean to have color with grep. If the underlying terminal emulator has capabilities, but the TERM environment variable is set to a monochrome terminal, you can use grep --color=always like with ls.
    – vera
    Jan 29, 2018 at 7:49
  • Yes, but i get colors even with ls --color=auto -l and just ls -l.
    – Ringger81
    Jan 29, 2018 at 10:32
  • This is the normal behavior as --color=always is the default behavior of ls (see man page). And like I I already said, ls --color=auto includes color unless you redirect output to file or pipe. Considering grep the man page does not explicitly provides the default behavior. But as the order of color values are never, always, auto, I guess the default behavior is never. Moreover you can do : grep --color=auto and you will see the same behavior than ls --color=auto. Consequently the behavior of --color of ls and grep is the same. The main difference is the default value.
    – vera
    Jan 29, 2018 at 13:12
  • You said before 'If the underlying terminal emulator has capabilities, but the TERM environment variable is set to a monochrome terminal, you can use grep --color=always like with ls'. Does it mean that the TERM var has no effect at all?
    – Ringger81
    Jan 29, 2018 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .