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

8

1

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?

Mikey T.K.

Posted 2013-09-24T16:15:28.550

Reputation: 3 224

isn't uname -o enough? ps: Osx has Darwin roots. – Lorenzo Von Matterhorn – 2013-09-24T16:16:40.680

2

Have you seen this answer on Stack Overflow? It looks like exactly what you're after.

– John Bensin – 2013-09-24T16:20:44.047

1-o is an illegal option on the uname that comes with Mac OS. See why this is such a problem? ;) – Mikey T.K. – 2013-09-24T16:20:53.467

@JohnBensin That'll do! Could you make that an answer so the appropriate points are awarded? – Mikey T.K. – 2013-09-24T16:27:05.993

1Since 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 – 2013-09-24T16:41:50.063

1as 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. – Lorenzo Von Matterhorn – 2013-09-24T16:43:39.290

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. – John Bensin – 2013-09-24T18:45:45.343

Answers

13

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

John Bensin

Posted 2013-09-24T16:15:28.550

Reputation: 1 355

0

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.

Andrei Neculau

Posted 2013-09-24T16:15:28.550

Reputation: 101