7

Using bash, I want to find the operating system and notify the user. I tried:

OS='uname -s'
echo "$OS"
if [ "$OS" == 'Linux' ]; then
    echo "Linux"
else 
    echo "Not Linux"
fi

I just get

uname -s 
Not Linux

on the terminal, which is wrong. How do I correctly set the string to what uname returns?

Thanks

Graham
  • 541
  • 2
  • 5
  • 13

2 Answers2

13

Rather than single quotes, you probably meant to use backticks:

OS=`uname -s`

but you really want

OS=$(uname -s)

Also, rather than an if statment, which will eventually become an if/else series, you might consider using case:

case $( uname -s ) in
Linux) echo Linux;;
*)     echo other;;
esac
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thanks for the answer! That is the first time I've ever noticed the difference between a ` and a ' :D – Graham May 24 '12 at 12:27
  • 1
    That's just one reason `$()` is superior :) – chepner May 24 '12 at 12:44
  • Oblig. quoting suggestion: `case "$(uname -s)"` in case the output contains spaces (although perhaps that is exceedingly unlikely). – chepner May 24 '12 at 12:46
  • 1
    @chepner. The quotes are not necessary, even if the output contains spaces. `$()` is a quoting mechanism, so putting it in double quotes is redundant. Consider: a='foo bar'; case $( echo foo bar ) in; $a):;; esac – William Pursell May 24 '12 at 12:50
  • 1
    `$()` only quotes when used immediately to the right of `=` in a variable assignment. Try `/bin/ls $(date)` to see the lack of quoting by `$()`. This probably doesn't affect the answer given above much, since `uname -s` almost always outputs a single word (without spaces). – Francis Litterio May 24 '12 at 14:50
  • 1
    $() doesn't perform the quoting at all. Bash does not word-split on the right side of a variable assignment. – jordanm May 24 '12 at 18:29
  • ... Nor on the argument to `case` which is why you don't need quotes there. – tripleee Oct 22 '14 at 06:25
  • Indeed, the expression "`$()` is a quoting mechanism" is complete nonsense. Bash doesn't word split the RHS of an assignment or the argument of `case`[ – William Pursell Oct 22 '14 at 17:04
2

This will return the OS as requested - note that uname is not necessarily available on all OSes so it's not part of this answer.

case "$OSTYPE" in
  linux*)   echo "linux" ;;
  darwin*)  echo "mac" ;; 
  msys*)    echo "windows" ;;
  solaris*) echo "solaris" ;;
  bsd*)     echo "bsd" ;;
  *)        echo "unknown" ;;
esac
rjmoggach
  • 1,458
  • 15
  • 27
  • -1 There is nothing Bash-specific here but it also does not solve the OP's problem at all. – tripleee Oct 15 '14 at 04:57
  • his question says "Using bash, I want to find the operating system and notify the user" `uname` is not OS agnostic - it won't work on some OSes. This solution should work on bash on most. You're correct that it's not bash specific so i've adjusted the answer. – rjmoggach Oct 22 '14 at 06:17
  • "$OSTYPE", distinguishes between android phones and full Linux installations. – Alejandro Mar 27 '16 at 21:52