Why not just setf / setb, and they always set the foreground and background colors are actually two questions!
In the first part, why ANSI and non-ANSI commands exist are too long for exaplin, and this is not necessary, as the story is pretty well explained on Wikipedia.
The second part, perhaps, can be freely rephrased to "what's the difference?" or "what can I do with this?"
Difference: ANSI terminals use a different mapping between color number and colors than non-ANSI terminals. For example, the code for yellow on one will be blue on the other. There are only two mapping tables. These things are described pretty well on Wikipedia.
What you can do with this: Find out what type of terminal you have and use the appropriate command. Or change your termcap. However, unfortunately, none of these solutions is completely general.
source share