2

I change shell's foreground color with echo -ne '\e[1;31m' command but when I execute ls command the foreground color reverts back to the original previous color. This weird behavior doesn't occur when other commands are executed. I am clueless about the cause behind this.

Please write an explanation for this and also suggest the remedy. thanks

I am using UBUNTU 14.10.

EDIT- I have tried to remove the escape sequence for white color foreground by setting the alias for ls as:

alias ls='ls --color=auto |sed -r 's/\x1b[37m]//''

But, this has a problem too, the ls outputs all the directory names and filenames with single color ,i.e not with the default colors for all items.This is strange as I have removed just the [37m which is the white foreground but it removes all the colors.

  • 1
    Could it be something to do with there being a predefined alias for ls which is ls --colour=auto? There's also an alias for grep with an auto colour option, so maybe grep would exhibit the same behaviour. You could always try unsetting the ls alias to see if it helps. – Arronical Aug 19 '15 at 16:29
  • @Arronical Thanks for the quick reply. Actually commenting out the ls --color=auto line does it but it removes the actual colored output of ls command . ls outputs the directory name and file name all with the same color . – 0decimal0 Aug 19 '15 at 16:37
1

Commands which set colours may finish by sending the sequence ESC [ 0 m to reset the current attributes for the terminal. The easiest thing for you to do is change your shell prompt to always set the colour back to what you want.

Add to your .bash_profile:

export PS1='\e[1;31m'"$PS1"
  • The export you suggested , will it set the color of the prompt or of the whole foreground ? I mean the text color like the color when I type . – 0decimal0 Aug 19 '15 at 16:41
  • I copied your own chosen escape sequence, which is the text foreground. – meuh Aug 19 '15 at 16:44
  • 1
    If you do script foo.txt, from there until you type exit, all I/O will be captured in foo,txt. Examine it with od -bc foo.txt and look at the ESCape sequences. – waltinator Aug 19 '15 at 16:51
  • 1
    man console_codes will help. The only suggestion I have for eliminating the suspect sequence is to start with the end of output (in the foo.txt file) and understand each ESCape sequence, and how it got produced. When you find that you have understood how the sequence you want to eliminate was produced, fix that part. – waltinator Aug 20 '15 at 6:07
  • 1
    Read man bash, the section on "PROMPTING", and read man console_codes. Let's take ls out of the equation. Do the script/od -bc thing with echo "my output ends after this LF" replacing the ls. The od -bc output should show "L" "F" "a linefeed character" and that is all the echo outputs. Subsequent characters come from your prompt. – waltinator Aug 20 '15 at 15:37
-1

UPDATE- As waltinator says executing bash command right after ls command will create child shell which will probably eat up resources , I had to look for another solution. I tried executing echo -ne '\e[1;31m' light after ls command therefore I put it in the bls funciton.

I somehow reached to my own solution to this problem. The suggestions by meuh and waltinator were to look for escape sequences sent by ls command and I even found out those. I tried to remove escape sequence by using sed but due to my inexperience at this I was not able to use it perfectly perhaps.

Later,I noticed that executing bash after ls command reverts the foreground back to the color I wanted.I decided against using bash after ls

Meanwhile , I found a way as :

  1. First of all I put the command echo -ne '\e[0;31m' in a shell script and add the shell script to .bashrc.
  2. Second, I defined a function so that bash would be executed right after ls as :

bls( ) {

 ls "$@" && echo -ne '\e[1;31m';
 }
  1. I exported the above function as :

    export -f bls

So, Now I have to do a bls for listing directories and not changing the foreground color. This didn't actually solve the ls problem but it did find me another way. I just have to type one more letter in bls

  • This does not do what you think it does. The bash commands spawns another shell process as a child of the current one. It takes over STDIN, STDOUT and STDERR, and reads the startup files (see man bash). To see your problem, type ps -fu $USER and look at all the bash processes. You will eventually exhaust some resource and be unhappy. – waltinator Aug 20 '15 at 20:59
  • Could you add the results of alias ls;env | egrep '^LS_' ; echo "$PS1" | od -bc to your question? – waltinator Aug 20 '15 at 21:06
  • LS_COLORS=fi=01;32:di=01;34:ln=00;38:*.txt=01;37:*.tgz=01;31:*.zip=00;33:*.z=01;36:*.html=00;32:*.png=01;35:*.deb=00;35:*.sh=01;31:*..sh=01;31: 0000000 133 134 165 100 134 150 072 040 134 100 040 134 167 135 040 110 [ \ u @ \ h : \ @ \ w ] H 0000020 165 153 165 155 040 155 145 162 145 040 141 153 141 134 044 040 u k u m m e r e a k a \ $ 0000040 012 \n 0000041 – 0decimal0 Aug 21 '15 at 8:10
  • This is the output of the above command , I am not adept at shell scripting , unix and linux , most of the things I do just by looking others.I would welcome suggestions from you on how to become an expert at this. I knew that this is not a solution of the real problem just an other way around. – 0decimal0 Aug 21 '15 at 8:10
  • @waltinator Please take the downvote back if you think that my updated solution won't cause any problem and I would appreciate if you add a solution as an answer I would accept that . Thanks :) – 0decimal0 Aug 21 '15 at 8:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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