There are several aspects at play in what you're asking.
First, bash doesn't define colors. In fact bash has absolutely no idea that colors even exist. All it knows is that you told it to output the characters \033[0;36m
. Your terminal emulator (xterm, gnome-terminal, whatever) receives these characters and understands "I need to start outputting in cyan".
Thus it is your terminal emulator that understands colors. Your terminal emulator understands that \033[0;36m
is cyan, but another terminal emulator might use an entirely different set of characters for cyan (though no sane terminal emulator would flaunt the standard and do this). This is the reason for tput
. When you run tput setaf 6
, tput
is going to look up your terminal's escape codes for the color 6 (cyan), and output that escape code.
(see this question for more info on tput setaf
codes)
Now back to bash. As you may have noticed, when I've been referring to the cyan color, I've been using \033[0;36m
, not \[\033[0;36m\]
. The square brackets have been missing. The purpose of the square brackets is that when using escape codes (colors) in the prompt, bash has to know which characters are non-printing (zero-width, don't actually show anything). Thus you enclose non-printing characters in \[
\]
. If you remove these characters, everything might appear to work just fine at first, but you'll start running into all sorts of weirdness when your command exceeds the terminal width. This is because when typing, bash has to know when the command should wrap to the next line. To do this, it calculates the width of the prompt, and then the width of how much you've typed.
Another note, about tput
. CYAN="\[\033[0;36m\]"
is not the same thing as CYAN="$(tput setaf 6)"
. As we just discussed, the square brackets are relevant to bash, and tput
is only going to output the terminal escape codes.
Since square brackets are usually only relevant in the prompt, if you're using colors in the output of a script or something, you should not use them. Meaning that if you're going to be using colors for more than prompts, you need to define multiple variables. One with square brackets for using in the prompt, and one without for everything else. Though you could just manually add the square brackets every time you reference a color in the prompt.
So long story short, you probably want to define something like:
local CYAN="$(tput setaf 6)" # OR CYAN="\033[0;36m"
local LIGHT_CYAN="$CYAN$(tput bold)" # OR LIGHT_CYAN="\033[1;36m"
local PROMPT_CYAN="\[$CYAN\]"
local PROMPT_LIGHT_CYAN="\[$LIGHT_CYAN\]"
ASCII characters are characters in the range from 0 to 177 (octal) inclusively.
To delete characters outside of this range in a file, use
LC_ALL=C tr -dc '\0-\177' <file >newfile
The tr
command is a utility that works on single characters, either substituting them with other single characters (transliteration), deleting them, or compressing runs of the same character into a single character.
The command above would read from file
and write the modified content to newfile
. The -d
option to tr
makes the utility delete characters (instead of transliterating them), and -c
makes it consider characters outside the given interval (instead of inside).
LC_ALL=C
makes sure that every byte value makes up a valid character. Without it, some tr
implementations would abort if they found sequences of bytes that don't form valid characters in the locale's character encoding.
To replace the original file with the modified one, use
LC_ALL=C tr -dc '\0-\177' <file >newfile &&
mv newfile file
This renames the new file to the name of the old file after tr
has completed successfully. If tr
does not complete successfully, either because it could not read the original file or not write to the new file, the original file will be left unchanged.
Alternatively, to preserve as much as possible of the meta data (permissions etc.) of the original file, use
cp file tmpfile &&
LC_ALL=C tr -dc '\0-\177' <tmpfile >file &&
rm tmpfile
Best Answer
I wrote a document in 1984 that summarizes ANSI Codes X3.64-1979, ANSI X3.4-1977, and ANSI X3.41-1974. This ansicode.txt describes how the control codes affect DEC LA-series hardcopy terminals and the VT-series video terminals.