1

I was wondering if any of you knew why doesn't my terminal keep displaying stuff colored, after I use the command ls -l -a --color=always I would like it to stay colored, so when the next time I type ls -l -a it is colored.

Just to make it clear, I'm using Windows 10, and then with putty I SSH into a server, where I have an account, and it runs Linux.

MatX
  • 11

1 Answers1

1

Once the output of ls is on the terminal, it stays colored. But if you run ls again, whether the output is colored depends on the options you pass to ls this time. The ls command doesn't remember settings from one time to the next.

If you want to have default settings for a command, define an alias for it. For bash, the file where aliases are defined is .bashrc. So add the following line to your .bashrc:

alias ls='ls --color=auto'

In addition, bash doesn't read .bashrc if it's a login shell, only if it's an interactive non-login shell. To get the same interactive configuration in both cases, put the following line in your .bash_profile:

if [ -e ~/.profile ]; then . ~/.profile; fi
case $- in *i*) . ~/.bashrc;; esac   # Load .bashrc if this login shell is interactive

For future customizations, use .profile or .bash_profile for session startup things like environment variables and .bashrc for interactive customizations such as aliases and shopt settings

If you ever want to run the ls program and bypass your alias, run \ls instead of ls.

  • Thank you for your advice. I did not find any .bashrc file in my user folder, so I created one, and inserted the line of code you suggested. Afterwards, it still remained monochrome instead of colorful. – MatX Apr 25 '16 at 10:04
  • @MatX What is the output of alias in your shell? Is your shell indeed bash (that's the default on most Linux systems, check the output of ps $$)? Does ls --color=auto output colors? – Gilles 'SO- stop being evil' Apr 25 '16 at 12:20
  • This is the result of the alias command:

    alias lp='lp -o nobanner' alias lpr='lpr -h' alias md5sum='/usr/local/bin/md5' alias quota='/usr/local/bin/quota -v' alias tex2='export PATH=echo $PATH | sed -e "s/\/opt\/teTeX3\//\/opt\/teTeX2\//g" ; export MANPATH=echo $MANPATH | sed -e "s/\/opt\/teTeX3\//\/opt\/teTeX2\//g"' alias tex3='export PATH=echo $PATH | sed -e "s/\/opt\/teTeX2\//\/opt\/teTeX3\//g" ; export MANPATH=echo $MANPATH | sed -e "s/\/opt\/teTeX2\//\/opt\/teTeX3\//g"'

    – MatX Apr 25 '16 at 17:24
  • @MatX The alias isn't present, so either you made a mistake when creating .bashrc, or your shell isn't bash. What's the output of ps $$; cat ~/.bashrc ? – Gilles 'SO- stop being evil' Apr 25 '16 at 17:36
  • @MatX Oh, and did you start a new shell? The rc file is only read when the shell starts. – Gilles 'SO- stop being evil' Apr 25 '16 at 17:37
  • Thank you for reminding. The result of ps $$; cat ~/.bashrc is the following: PID TT S TIME COMMAND 14269 pts/18 S 0:00 -bash alias ls='ls --color=always' And yes, the ls --color=auto command also outputs colors, and yes, I restarted the shell session. Maybe I'm deprived of some rights by the admins? Is that possible? Or does it have to do anything with my default putty settings? – MatX Apr 25 '16 at 20:48
  • @MatX Ah, yes, of course, you're logging in over SSH, so you're getting a login shell, and bash unhelpfully loads a different file in that case. Sorry, I should have thought of that, it comes up all the time. See my edit. – Gilles 'SO- stop being evil' Apr 25 '16 at 21:04
  • Thank you, man! I applied what you suggested, and it finally solved my problem along with other issues. – MatX Apr 25 '16 at 21:36