How to detect ubuntu in a shared bashrc?

1

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.

Capi Etheriel

Posted 2011-03-10T14:40:24.520

Reputation: 668

Answers

1

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.

l0b0

Posted 2011-03-10T14:40:24.520

Reputation: 6 306

uname -v gives the kernel version details. It's likely that running an Ubuntu-branded kernel will mean that you are running on Ubuntu, but it's perfectly plausible to be running a non-Ubuntu-branded kernel on an otherwise Ubuntu system. – a CVn – 2017-02-17T13:37:48.290

At that point you'd have to define sharp boundaries between distros, which is basically like trying to define life or intelligence. – l0b0 – 2017-02-17T14:19:19.993

1

  1. lsb_release -i should give the distribution/distributor ID directly. For instance we can do

    raub@desktop:/tmp$ lsb_release -i
    Distributor ID: Ubuntu
    raub@desktop:/tmp$ 

    in an ubuntu box or

    [raub@otherdesktop ~]$ lsb_release -i
    Distributor ID: CentOS
    [raub@otherdesktop ~]$ 
    

    in a centos box. Then you can do more interesting stuff like

    raub@desktop:/tmp$ lsb_release -i | awk '{ print $3}' | tr 'A-Z' 'a-z'
    ubuntu
    raub@desktop:/tmp$
    

    which can be then added to a variable and used somewhere else, like user1179239's example.

  2. If you do not want to use lsb_release, try /etc/issue

    [raub@otherdesktop ~]$ cat /etc/issue
    CentOS release 6.8 (Final)
    Kernel \r on an \m
    
    [raub@otherdesktop ~]$
    raub@desktop:/tmp$ cat /etc/issue
    Ubuntu 16.04.1 LTS \n \l
    
    raub@desktop:/tmp$

raubvogel

Posted 2011-03-10T14:40:24.520

Reputation: 11

1

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

file=~/.bashrc-$HOSTNAME
if [[ -f $file ]]; then
    . "$file"
end

user1686

Posted 2011-03-10T14:40:24.520

Reputation: 283 655

but i'm sharing on github, so it'd be nice if i could think of OS. – Capi Etheriel – 2011-03-12T04:38:00.193

@barraponto: in that case it's "Linux" and "Linux". – user1686 – 2011-03-12T14:04:13.943

1

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

user1179239

Posted 2011-03-10T14:40:24.520

Reputation: 220

Alternatively, if grep -iq ubuntu /etc/lsb-release – a CVn – 2017-02-17T13:38:57.470