-2

Is it possible to turn off colors for a specific command like ls, but without using the api of the command itself? Is there an env variable kind of thing that will turn off coloring for a command like doing COLOR=off ls? Any solution would be highly appreciated.

Edit: I want a general solution that will be applicable to all commands. like COLOR=off command_name

2 Answers2

3

If you are willing to allow some changes in the formatting, you may as well just pipe it through cat

 ls | cat

No colours, but newline for each entry.


Edit:

Turning off colors via the shell settings

Check your shell's rc file(s) and see where colors are set, then make a new .<shell>rc_mono where these settings are removed, source it and done. E.g. for bash:

$cat ~/.bashrc_mono
export TERM=xterm-mono
. ~/.bashrc
alias ls='ls --color=none'
alias grep='grep --color=none'

Note that . ~/.bashrc must precede the new aliases for ls and grep as their coloring usually is set in ~/.bashrc. So all you need to do is

. ~/bashrc_mono ; ls 

Accordingly a "redo colors"-rc might be nice:

$cat ~/.bashrc_recolorize
export TERM=xterm-256color
. ~/.bashrc

Use e.g. htop and you will see the difference.

FelixJN
  • 13,566
  • I've updated the question. Please check it out. – KMA Badshah Jun 15 '21 at 17:00
  • @KMABadshah This answer is not specific for ls but should work for all commands that automatically switch on colors if the output goes to a terminal. – Bodo Jun 15 '21 at 18:00
1

One option would be to set the TERM variable to a terminal type that doesn't support colors.

Examples:

TERM=dumb ls
TERM=dumb command_name
echo foo | TERM=dumb grep --color o

Telling the programs to use a dumb terminal might also have other (unwanted) effects depending on the command you run, for example vi may no longer recognize the cursor keys.

As suggested by FelixJN you can also try TERM=xterm-mono, but on my test system (Git bash on Windows) this doesn't suppress colors in contrast to TERM=dumb.

Bodo
  • 6,068
  • 17
  • 27
  • 1
    TERM=xterm-mono is not as dumb as TERM=dumb - at least for cursors in vim. PS: beat me with the TERM-setting option, though specifying it specifically for the command is better than a new rc-file. – FelixJN Jun 15 '21 at 18:26