Search code examples
linuxbashuname

Why am I getting an error when I use `uname` in my .bashrc?


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?


Solution

  • 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.