6

I have to differentiate command from the output using color

like this

$ cat file-name (this should be in some color say like red)
some test in file OUTPUT (this output should be in some other color say like green) 

so that I can easy differentiate command from its output in case of commands with large output

And i have to make it it work for every command that is typed on terminal if possible even command not found can be printed out in different color

6

Add this to your ~/.bashrc:

        RED="\[\033[0;31m\]"
     YELLOW="\[\033[1;33m\]"
      GREEN="\[\033[0;32m\]"
       BLUE="\[\033[1;34m\]"
  LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
       CYAN="\[\033[0;36m\]"
 LIGHT_CYAN="\[\033[1;36m\]"
      WHITE="\[\033[1;37m\]"
 LIGHT_GRAY="\[\033[0;37m\]"
 COLOR_NONE="\[\e[0m\]"

PS1="$PS1${LIGHT_RED}"
trap '[[ -t 1 ]] && tput sgr0' DEBUG

The colour code variables aren't necessary, but they simplify things. Change the LIGHT_RED to other colours to suit your need. Effect:

RED BLUE

You'll have to source the .bashrc for changes to take effect:

. .bashrc

The LIGHT_RED isn't really light red for me because of the colour profile I have set for the terminal.

Sources:

  1. How do I stop a bash shell PS1 color to stop at the end of the command?
  2. Bash command prompt with virtualenv and git branch (for the colours)
  • yes this one works the way i wanted it – user310685 Sep 12 '14 at 11:30
  • but with this i am able to color the command but i am unable color the output of the command i used this for that trap '[[ -t 1 ]] && tput setaf 2' DEBUG – user310685 Sep 12 '14 at 11:54
  • @user310685 I'd usually use the terminal's colour settings to set the colour of the output. – muru Sep 12 '14 at 11:59
  • yes that works good – user310685 Sep 12 '14 at 12:16
0

you could try this

grep --color=always . file-name

The dot is essential in this case, it's the regular expression for matching any single character.

  • how shall i try this – user310685 Sep 12 '14 at 9:13
  • 1
    but i guess this wont work for other commands like (ls) – user310685 Sep 12 '14 at 9:14
  • @user310685 use this command in your putty, and yes this is for replacing 'cat' it won't work with ls. – P.-H. Lin Sep 12 '14 at 9:32
  • i wanted to make it work for each and every command – user310685 Sep 12 '14 at 10:33
0

You can use ANSI escape sequence to differentiate the command from its output, like this:

echo -e "\e[31m"; echo "ls -al"; echo -e "\e[0m"; echo -e "\e[32m"; ls -al --color=never ; echo -e "\e[0m";

enter image description here

Source: http://misc.flogisoft.com/bash/tip_colors_and_formatting

  • this does not work – user310685 Sep 12 '14 at 9:42
  • i guess there is some difference in version that y it dose not work for me , but i guess making it work will not make it work for every command. i have to manually add it into every command – user310685 Sep 12 '14 at 10:35

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.