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.
2 Answers
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" 13 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 :
- First of all I put the command
echo -ne '\e[0;31m'in a shell script and add the shell script to.bashrc. - Second, I defined a function so that
bashwould be executed right afterlsas :
bls( ) {
ls "$@" && echo -ne '\e[1;31m'; }
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