-2

Long time Mac (read GUI) user who recently started running Ubuntu and has spent a week or so customizing my .bashrc (Ubuntu) file and .bash_profile (Mac) for informative and colorful PS1 and terminal output.

I've got this tiny little function:

function colors2nums()
{ for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done }

that displays 256 colors with codes so I can choose the one I want...

It works perfectly in Ubuntu (14.0.4 LTS, Gnome terminal) and does not work at all on the Mac (OS 10.7.5 both iTerm & Terminal.app).

The PROMPT_COMMAND function of the .bashrc file on Ubuntu is exactly the same, copied & pasted line-for-line as the Mac's PROMPT_COMMAND function is in .bash_profile.

Both PS1 prompts are identical in color (with just some odd spacing on the Mac side) and ls -GFa (or any variant) prints in the colors set by LS_COLORS and .dircolors. So colors are working for some output, but not all. On the Mac, I get 256 lines in the same color as my terminal input set by the last escaped color (green, white, whatever) in my PS1.

WTH?!?

And yes, I've tried 2 variations of the trap - DEBUG (hack?):

#trap 'echo -ne \e[0m' DEBUG

trap 'printf "\e[0m" "$_"' DEBUG

that works to reset the output on the Ubuntu side of things. Neither works on the Mac.

What am I missing?

iMattux
  • 3
  • 2

2 Answers2

1

Having more than just a few colors in a terminal is a relatively recent "invention", and not all terminal programs are able to handle it.

Also note that it's the actual terminal program that handles it, the shell has nothing to do with it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @iMattux Yes, but Bash is a *shell* that runs inside a terminal emulator program (e.g. `gnome-terminal`). It's the terminal emulator program that sends input to the shell, and prints the output from the shell. – Some programmer dude Sep 22 '15 at 19:54
0

Try using printf instead:

for code in {0..255}; do printf "\e[38;05;${code}m $code: Test"; done

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks Mark! printf worked. Now here's a strange discovery I'll share: I actually got it to work late last night by using different escape sequences. I found this site [link](http://jafrog.com/2013/11/23/colors-in-terminal.html) and she mentioned 3 different escape sequences: \e (shell), \0x1b (ASCII hex) and \033 (ASCII Oct). Both the Hex and Oct variations worked as well. I'd love to know why, but for now, it works and I'll move on to other things. Cheers! – iMattux Sep 22 '15 at 17:23
  • New to Stack Exchange, sorry for the poor etiquette and delayed acceptance. – iMattux Sep 23 '15 at 20:39
  • No probs! I think you get points for accepting too:-) Welcome to SO and enjoy! – Mark Setchell Sep 23 '15 at 21:05
  • If you're using `printf`, you don't need the `for` loop: it will repeat as necessary for all the arguments: `printf "\e[38;05;%dm $code: Test" {0..255}`. It is worth mentioning that this is completely specific to the type of terminal that's connected to standard output - they are not all the same. – Toby Speight May 05 '17 at 10:54