How do you determine if you're on a Linux or BSD system inside a script?

I've got a rather extensive selection of dotfiles thats been kept in a git repo for the longest time - I just maintain a separate branch of code for bsd-isms (for instance, on Linux, to get colored ls output you do ls --color=auto, but on BSD-likes including Mac OS, it's ls -g).

This has started to become unwieldy since I have to make the change, then merge branches and deal with conflict resolution every time I touch my shell's rc file. Figure the proper way to handle this would be to drop the git branching, and just have my rc file determine what kind of OS its on, and then source an appropriate addon script for the aliases.

Problem is, I have no idea how to do this.

How do I tell if I'm on Linux or BSD from within a script or shell rc file?

uname won't really work since then I have to write a bunch of logic for the various flavors of systems out there. Mac says Darwin, FreeBSD, etc, when I'm more interested in the underlying type of OS since that will impact what particular set of tools I'm dealing with. For my example above, am I using bsd ls or GNU ls?


This Stack Overflow answer by Nicolas Martyanoff provides a complete solution. I tweaked it to use the newer syntax mentioned in the comments.

Determine the OS:

platform='unknown'
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
   platform='linux'
elif [ "$unamestr" = 'FreeBSD' ]; then
   platform='freebsd'
fi

Choose the right flags for ls:

if [ "$platform" = 'linux' ]; then

   alias ls='ls --color=auto'

elif [ "$platform" = 'freebsd' ]; then

   alias ls='ls -G'

fi