I have PS1 that ends with an opening color sequence (like \[\e[0;32m\]) to have all text in the terminal colored (green in this case). However, when I use a command such as ls (which is aliased to ls --color=auto in my .bashrc) the colors in the output are a little messed up: green up to the first "colored" word, and the following uncolored text is white instead of green.

Is there a way to have clean output in all cases? I'm okay with white as "default" text color when the output has colors, but I'd like it to be consistent.

Of course, I'd most like a solution that would automatically apply to all commands and that wouldn't require me to change the way I invoke them.

Personally, I don't have any ideas. Maybe something can be done to search for escape sequences in the output "on the fly" and perform some substitutions? I don't know how to implement this, though, especially so that it's done "behind the scenes".

  • 2
    What terminal are you using? It will be easier to just set the default colors in the terminal's configuration.
    – Random832
    Jun 8 '12 at 15:45
  • @Random832 I'm using guake, but the thing is I want the colors to be bound to the specific machine, so that I see it the same when I connect via ssh from a computer with different local settings. Jun 8 '12 at 18:05
  • Does the escape sequence \e]10;#009900\e\\ do anything? On some terminals (xterm), this will set the default foreground color at runtime... if so you could have it sent at the beginning of your ssh session and back to the default when you leave it with a shell script to wrap around ssh.
    – Random832
    Jun 8 '12 at 18:18
  • @Random832 Um, what's the right way to check? Jun 8 '12 at 18:28
  • 1
    If you want to set it up, there's a program called grc (Generic Colouriser) that lets you re-colour command output based on regexes. (the program) korpus.juls.savba.sk/~garabik/software/grc.html (further explanation) wynnnetherland.com/journal/… Jun 9 '12 at 1:40

The reason is because you're doing it wrong.

You said that you have an "open" color sequence at the end of your prompt. This is wrong. Colors do not nest. There's no "open" and "close". It's "switch to ..." or "reset to default" (which is actually "switch to 0"). So when ls --color=auto switches color for something when it's done it will issue the sequence to reset to the default. It's not "go back to what it was before".

Set the terminal to use the color of text that you want to be "default" (i.e., palette number 0). Then if you want your prompt a different color set it at the beginning and a reset at the end.

For more information read the Bash Prompt HOWTO Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement documentation.

  • See my comment above, I'd like to connect from the same terminal to different machines and see different color schemes. Any tips on how to achieve that better than I do it now? Jun 8 '12 at 18:10
  • You need the "default" to be consistent across all machines. When a color gets set it's not really specifying a particular color. It's selecting an item from the color palette. The palette is defined in your term application.
    – bahamat
    Jun 8 '12 at 18:32
  • I was hoping to have them different - white and some and green at others. Jun 8 '12 at 18:35
  • Unfortunately that's not how it works. You may be able to find a terminal application that can do that for you, or you could define a new set of DIR_COLORS on each system. But there are more things than just ls that use coloring. You'd have to change each of them.
    – bahamat
    Jun 8 '12 at 18:43

As answered before, having a PS1 ending with an opening color sequence is not a good idea. This will render the output of colored commands wrong. If you want to have your terminal foreground color (the normal text color) in green, simply set it to green in your terminal emulator settings on both machines - in this case your guake terminal settings. This way the foreground color stays green and the commands with colored output will use this color to display normal text. Reset your PS1 prompt like it should be done with \[\e[0m\].

  • But I want the default foreground to be white on my local machine and green on the remote one. So that I don't mix them up when having several terminals open. Jun 9 '12 at 12:22
  • @LevLevitsky Well, then you can leave the PS1 unresetted on your remote, and not use output coloring with commands - you can't have consistency with both things enabled. Or, like many others, you could have a differnt/informative PS1 with other colors than your local PS1 - ie. showing what host you're on.
    – user13742
    Jun 9 '12 at 12:27
  • It does show the host, but I actually need to tell from more than two machines, and I want them to be as different as possible. Jun 9 '12 at 12:31
  • You have gotten answers to your original question. For the latter, it's a matter of preference.
    – user13742
    Jun 9 '12 at 12:36

Here's how I do different colored prompts for different machines.

case $HOSTNAME in
    host1*) PSC="\e[32m" ;;
    host2*) PSC="\e[33m" ;;
    host3*) PSC="\e[34m" ;;
    host4*) PSC="\e[35m" ;;
    *) PSC="\e[36m" ;;

PS1="[\j]\[${PSC}\]\u@\h(\l) \[\e[37m\][ \w ]\[\e[00m\]\n\[\e[1m\]\#\[\e[0m\] \$ "

The rest you can suite to taste. Put a copy of this ~/.bashrc on every machine you use. Of if you use a NFS mounted home it's just automatic.

  • Nice different-host-PS1 solution, but it doesn't answer the question.
    – user13742
    Jun 9 '12 at 13:20
  • The OP mentioned in a comment that this is what he was really after.
    – Keith
    Jun 10 '12 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.