0

I am trying to consolidate my dotfiles so I only have one set for my local OSX machine and Linux servers. I have this conditional statement, but the $(brew --prefix) is running on linux machines even though that block shouldn't run...

SYSTEM=`uname -a | cut -d" " -f1`
# These things are system specific
if [ $SYSTEM=="Darwin" ]; then
    if [ -f $(brew --prefix)/etc/bash_completion ]; then
        . $(brew --prefix)/etc/bash_completion
    fi

    for file in ~/.completion/* ; do
      if [ -f "$file" ] ; then
        . "$file"
      fi
    done

elif [ $SYSTEM=="Linux" ]; then
    alias upgrade='sudo apt-get update && sudo apt-get upgrade'

    # save the IP address in case I need it later
    export IP_ADDRESS=`ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
fi

if I echo the $SYSTEM variable on the linux machine it says it is Linux but sourcing of the bashrc fails on the brew --prefix command.

Does it evaluate that variable no matter what? Is there a way I can change it to work?

Joff
  • 11,247
  • 16
  • 60
  • 103
  • Try spaces around the `==`. Also note https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash –  Sep 30 '17 at 02:47
  • Tip: [ShellCheck](http://www.shellcheck.net) will automatically identify [this issue](https://github.com/koalaman/shellcheck/wiki/SC2077) and other common issues. – that other guy Sep 30 '17 at 04:13

1 Answers1

3

You need spaces around the ==:

if [ $SYSTEM == "Darwin" ]

Otherwise bash behaves as if you had written:

if [ "some-non-empty-string" ]

which is always true, as opposed to:

if [ "" ]

which is always false.

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • Also, a single equal-sign (`=`) is standard in `[ ]`. bash allows `==` as a synonym, but it won't necessarily work in other shells. And it's always good practice to double-quote variable references (e.g. `if [ "$SYSTEM" = "Darwin" ]`) – Gordon Davisson Sep 30 '17 at 04:15