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?

  • isn't uname -o enough? ps: Osx has Darwin roots. Sep 24, 2013 at 16:16
  • 2
    Have you seen this answer on Stack Overflow? It looks like exactly what you're after. Sep 24, 2013 at 16:20
  • 1
    Since question is still on topic here, there's no harm in it staying. But if someone wants to quote some of that SO answer, you have to link back and mention the author of the answer. And you probably don't need to quote the whole answer
    – random
    Sep 24, 2013 at 16:41
  • 1
    as for me, i wont link it inside a new answer and take points from someone else's work. as long as its added as a comment is fine by me. @random thank you for the detailed explanation on how to react. Sep 24, 2013 at 16:43
  • 2
    @LorenzoVonMatterhorn I added it as a CW answer. Comments aren't guaranteed to stick around, as they're second-class citizens on SE sites. Also, since links to other SE sites don't show up in the Related or Linked sidebar, a link buried in a comment thread isn't necessarily easy for future visitors to find, especially if they're not familiar with SE sites. The question just looks unanswered. Sep 24, 2013 at 18:45

2 Answers 2


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:

if [ "$unamestr" = 'Linux' ]; then
elif [ "$unamestr" = 'FreeBSD' ]; then

Choose the right flags for ls:

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

   alias ls='ls --color=auto'

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

   alias ls='ls -G'

  • As the Q says, uname is not sufficient because macos says Darwin. Sep 2, 2021 at 11:31
  • What about Windows using Git Bash...? Sep 23, 2022 at 20:04

Leaving aside the platform detection (correctly done via uname), it is not just the flags that need to be picked correctly but also the path to the executable.

Calling just ls -G means give me any alias/function/executable-found-in-$PATH and run it with the flag -G. That alias/function/executable might or might not be BSD on a BDS OS, might or might not follow BDS arguments, might or might not be the exact same version that you expect, etc.

For that reason, one would use the full path to the built-in executable that has a really high chance (fiddle with system-level is not as common as fiddling with aliases/functions/$PATH) of following the OS: /bin/ls -G.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .