-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

  • 3
    Read `man ls`. GNU `ls` has the `--color=never` option, and there's the `LS_COLORS` variable. – waltinator Jun 15 '21 at 12:25
  • Do add on to @waltinator's comment, you could in your shell config file have something like `alias ls='ls --color=never'` – Panki Jun 15 '21 at 13:13
  • [This question shows how to remove color codes through the use of text replacement.](https://unix.stackexchange.com/questions/4527/program-that-passes-stdin-to-stdout-with-color-codes-stripped) – Panki Jun 15 '21 at 13:14
  • At least on GNU/Linux systems, `ls` output is typically only colored in a terminal because `ls` is aliased to something like `ls --color=auto`. So to get uncolored output it is sufficient to prevent alias expansion ex. `\ls`. See also [How to turn off color with `ls`?](https://unix.stackexchange.com/questions/107371/how-to-turn-off-color-with-ls) – steeldriver Jun 15 '21 at 15:27
  • My question wasn't specific to `ls`. I want a general solution that will turn off colors for a command – KMA Badshah Jun 15 '21 at 16:58

2 Answers2

2

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
  • 12,616
  • 2
  • 27
  • 48
  • 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
  • 5,979
  • 16
  • 27
  • `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