I share my .bashrc (all my dotfiles, actually) between ubuntu and archlinux. Since the paths may vary between them, I'd like to run some commands only on ubuntu (and others only on archlinux). How can i detect whether I am running ubuntu inside the .bashrc file? Detecting Debian will probably work.

link|improve this question

feedback

3 Answers

up vote 1 down vote accepted

The result of uname -v on my machine contains "Ubuntu" - Maybe you can use that? However, a more portable solution would be to check for the existence of the paths instead:

FOO=/path/to/executable
if [ -x "$FOO" ]
then
    "$FOO" --option
fi

This is the standard way in GNU makefiles.

link|improve this answer
feedback

Think of it as different computers, not different operating systems:

file=~/.bashrc-$HOSTNAME
if [[ -f $file ]]; then
    . "$file"
end
link|improve this answer
but i'm sharing on github, so it'd be nice if i could think of OS. – barraponto Mar 12 '11 at 4:38
@barraponto: in that case it's "Linux" and "Linux". – grawity Mar 12 '11 at 14:04
feedback

REVISED: the original version doesn't work on Ubuntu 10.04 which does not mention Ubuntu in uname -v. The /etc/lsb-release file is much better for this purpose as it has an explicit DISTRIB_ID line set to Ubuntu.

Based on l0b0's response, this sh script detects Ubuntu with an if statement. As others have pointed out, depending on what you're doing it may be more appropriate to detect particular programs or features, but as someone who has written Ubuntu-specific installers I appreciate that sometimes a simple smoke test that someone is not misapplying them is all you want.

#!/bin/sh

UBUNTU=`grep -i ubuntu /etc/lsb-release | wc -l`
if [ "$UBUNTU" != "0" ] ; then
  echo "This is so totally Ubuntu!"
fi
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.