1

In my .bashrc I have the following code

if [`uname` == "Linux"]; then
    echo "It worked"
else
    echo "It didn't work"
fi

But when I source my .bashrc I get the following results

[Linux: command not found

It didn't work

Strangly, the [ is not a typo, it is part of the error. If I comment out the if-statement, then the error goes away, so I am pretty sure that it is the source of the error. Plus, if I change the Linux to linux, then the error changes to lowercase also.

And if I echo uname I get Linux.

To source my .bashrc I have used source .bashrc and also have started a new bash session by typing bash on the command line terminal.

I didn't think it was that hard to check for the OS type, but I can't seem to figure out the correct syntax for the .bashrc.

I don't see what I am doing wrong, can anyone help?

Community
  • 1
  • 1
Fred
  • 1,054
  • 1
  • 12
  • 32

2 Answers2

10

You forgot a space after the square brackets. The first line has to look like this:

if [ `uname` == "Linux" ]; then

In your version, without the spaces, the [ and the output of uname is concatenated into one executable named [Linux, which does not exist in the PATH.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Florian Sowade
  • 1,727
  • 12
  • 20
  • Stupid me. Thanks I should have caught that. – Fred May 30 '12 at 20:05
  • +1 for answering the OP's implied question of "why," and not just showing the correct syntax. The only thing I'd change is that Bash is not saying that `[Linux` doesn't exist; it's saying that it's not found in the PATH. Subtle distinction, but possibly important to someone other than the OP. – Todd A. Jacobs May 30 '12 at 20:22
  • @Florian - P.S. thank you for being nice about your answer. I have found on many forums people can be very mean to beginners like me when we make dumb mistakes. – Fred May 30 '12 at 22:26
4

Bash in finicky about spacing. You need spaces in your conditional

if [ `uname` == "Linux" ]; then
    echo "It worked"
else
    echo "It didn't work"
fi
Andy Jones
  • 6,205
  • 4
  • 31
  • 47