Can I use .bashrc to make ls show colors on both OS/X and Linux?
When you're trying to do something portably, test for features, not platforms:
if ls --help 2>&1 | grep -q -- --color
then
alias ls='ls --color=auto -F'
else
alias ls='ls -FG'
fi
Platform tests break when platforms change. macOS ships a mix of BSD and GNU userland tools today, but this mix is shifting over time towards a greater preponderance of BSD tools. So, a test for "macOS" today may fail tomorrow when Apple replaces a GNU tool you were depending on with its nearest BSD equivalent if you are relying on a feature the two implement differently. Feature tests more often keep working in the face of change.
As a bonus, you sometimes end up creating support for platforms you did not originally test against. The above script fragment should also do the right thing on Solaris and FreeBSD, for example.
(This is the philosophy behind GNU Autoconf, by the way, which is why a configure
script written 10 years ago probably still works on a brand new system today.)
Modify the aliases to suit. I'm just showing the values I use on the macOS and Linux systems nearest to hand as I write this.
you can check if it's linux, and then set ls
for it. ie:
export CLICOLORS=1
if uname -a | grep Linux >/dev/null; then
# your linux ls stuff
fi