0

I need to change the output color of stdout in terminal, That means I need to show the outputs in colored contents.

For that I have tried like this.

      Bhuvanesh: May$ tput setaf 3 ; ls 
       Bhuvanesh: May$ tput setaf 3 ; cat file

The above show the output in color, So I put that into my .bashrc file But it will not like what I expected.

How Can I do that???Please explain

1 Answer 1

1

You can use escape sequences for echo. For instancce, in the screenshot bellow you can see me set color first to red, then back to white, and source my .mkshrc file, where I've placed $( echo -e "\033[1;31m" ) before my username @ hostname, and $( echo -e "\033[1;37m" ) before the ***** line, and finally $(echo -e "\033[1;34m" ) after $ character. In Other words my prompt is:

[$(batpower)]$(echo -e "\033[1;31m") _MKSH_SERGIY@UBUNTU_[$(pwd)]$(echo -e "\033[1;37m"   )
***********************************************
! $ $(echo -e "\033[1;34m")'

More info here: www.tldp.org/LDP/abs/html/colorizing.html

and here:http://www.tldp.org/LDP/abs/html/colorizing.html

enter image description here

1
  • I'd recommend using tput instead of hardcoding the terminal escapes. Also, the $(echo -e ...) is redundant; PS1 will already treat \e as ESC. And lastly, lines will now wrap around incorrectly because you forgot to put the terminal escapes within \[ and \]. See BashFAQ 53. EDIT: missed the point this was in your .mkshrc, but this will break for bash, which is what the questioneer is using.
    – geirha
    May 11, 2015 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .