how to enable and disable colors in linux command shell

Many modern command shells and commands are capable of displaying its output in color. Most times it is a combination of the shell and the command that provides the ability to display the text in color. The terminal displays these colors in response to some ANSI encoded control sequences generated by the command as part of its output.

You can usually turn the colors off in a terminal which means none of the commands executed on that terminal will show colors. The option in xterm is -cm, and it is quite possible that the terminal that you use have similar options. Some terminal emulators like Konsole or Gnome-Terminal may require you to create a custom profile, with all or multiple color sequences mapped to the same color.

If it is not possible to turn off or disable colors in the terminal, you can use the command line options that are available with the commands. Most shell commands have the command line argument named –color that allow you to control the displayed colors.

ls command

The ls command which displays the contents of a folder is a popular command that supports colors in its output, mainly to distinguish different file types. The ls command has a command line argument –color that can be used to control the colorized display.

never: In order to disable the colors, use the never value to the color argument. This is the default value.

bash$ ls --color=never

auto: Setting the color argument to auto, means that the command will output color codes only on a terminal. The standard output or the stream that it is being used should be connected to a terminal.

always: As the value suggests, the color codes are always emitted.

By default, the ls command should not display colors. If you find that it is displaying colors, then you might want to check your command alias to make sure that it is not being set there. You can easily check it with the alias command, without any arguments. Use the unalias command to reset it

bash$ unalias ls

vi or vim editor

Another basic utility that displays color is the vi editor. You can turn off colors from within the editor, in command or escape mode. While you are in the editor, to enable colors or syntax highlighting hit the escape key and then type

:syntax on

To turn off the syntax highlighting, type

:syntax off

If you want to make the setting permanent then you can set the value in the vi configuration file. The local configuration file is named .vimrc and should exist in your home directory. If there is none, then you can create one and add either syntax on or syntax off in it.

grep command

grep is another commonly used Linux command which support color codes in its output. In terms of the options and the output, it is very similar to the ls command described above. In order enable or disable the colors, you can again use the command line argument –color

The –color argument again takes three values: never, auto and always.