Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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.

share|improve this answer

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

file=~/.bashrc-$HOSTNAME
if [[ -f $file ]]; then
    . "$file"
end
share|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

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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