Bash color shell on Mac?

8

5

How can you make a bash shell list executable files in a different color than non-executable files?

I've tried editing ~/.bashrc to contain the following line (it's otherwise empty):

 LS_OPTIONS='--color=auto'

But it's not working. What am I doing wrong? I'm working on Mac OS X.

AP257

Posted 2011-03-21T21:26:43.703

Reputation: 195

Answers

7

To turn on colour output from the ls command without having to create an alias to ls or download any additional software, add the following to your ~/.bash_profile:

# Terminal colours
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

If you don't like those colours you can use this ls color generator to customize that color list to your liking.

You'll need to do:

source ~/.bash_profile

After making any changes for them to take effect in your existing shell.

Ian C.

Posted 2011-03-21T21:26:43.703

Reputation: 5 383

no, it doesn't help at all – holms – 2013-11-16T03:24:47.383

6

The problem is that OS X doesn't have GNU ls; while its ls does support file name coloring, it can only do so by the type of file (file, directory, symlink, device special file, fifo, socket...). Install coreutils from Fink/MacPorts/HomeBrew, then use alias ls='gls --color=auto'.

geekosaur

Posted 2011-03-21T21:26:43.703

Reputation: 10 195

Thanks. I've installed coreutils from MacPorts. Should the alias line be in ~/.bashrc? That's where I've put it, but I'm not seeing any difference. – AP257 – 2011-03-22T23:10:39.157

~/.bash_aliases may work better. I also found that I needed to run eval $(gdircolors -s) to set it up. – geekosaur – 2011-03-22T23:12:20.813

@AP257: On my Mac, I put aliases into the .bash_profile – Hai Vu – 2011-03-29T02:56:59.373

@Hai Vu: That only works if you never use subshells. – geekosaur – 2011-03-29T02:58:53.917

@geekosaur gdircolors: invalid option -- 's' do you mean --sh ? – Anentropic – 2013-01-15T12:44:55.573

6

BSD ls works a bit differently

alias ls='ls -G'

should work.

And this isn't bash coloring, it is ls doing the colorization. When bash does a file list (try echo * in a directory) there is no way to colorize. Typing ls -G would work in any shell, though a shell (like bash) that has aliases makes it easier.

Rich Homolka

Posted 2011-03-21T21:26:43.703

Reputation: 27 121

6

On the Mac, you need to use

export CLICOLOR=1

I put that in the .bash_profile. However, I prefer Rich Homolka's solution to alias ls with -G flag. If you want to customize the colors:

man ls

and search for LSCOLORS

Hai Vu

Posted 2011-03-21T21:26:43.703

Reputation: 4 824

1

Consider installing brew and using the GNU version of ls and other tools.

Install XCode from the AppStore.

Install Homebrew...

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install coreutils...

brew install coreutils

Add to the bottom of your .profile...

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

I alias ls in my .profile to do colors for everything and a shortcut for full list.

alias ls='ls --color'
alias l='ls -lah'
# -l     use a long listing format
# -a     do not ignore entries starting with .
# -h     with -l and/or -s, print human readable sizes (e.g., 1K 234M 2G)

There are a bunch of LS_COLORS options on github.

Paul J

Posted 2011-03-21T21:26:43.703

Reputation: 198

-1

bash is only a shell, it does not list files. It executes commands, such as ls.

The --color option (and long options in general) is specific to the version of ls from GNU coreutils, which comes with most Linux distributions. On the other hand, Mac OS X has BSD roots and uses the BSD version of ls which does not support colouring.

user1686

Posted 2011-03-21T21:26:43.703

Reputation: 283 655

1-1: It does support colouring, just with a different command-line flag: -G. – Wuffers – 2011-03-29T02:58:34.793