1

I'm studying basic Bash scripting on a Debian System 8.6. I've read an example I found on a book that shows how to build a conditional construct. I let you it below:

#!/bin/bash
#Filname: CondConst.sh

if [$UID -ne 0]; then
        echo "Non root User."
else
        echo "Root User."
fi

When I run the script containing this code I get the following error message and result:

[1000: command not found
Root User.

I've tried to modify the code in many ways (using quotes and spaces), but I could not understand how to use square brackets syntax for the comparision. I worked out the issue by using a different syntax, that I show you below:

#!/bin/bash
#Filname: CondConst.sh

if (($UID!=0)); then
        echo "Non root User."
else
        echo "Root User."
fi

Can anyone explain me, if possible, the reason why I can not use square brackets?

Thank you, really, in advance.

1

Your error is because you are not typing a space after [ so it is see it as a one command [1000, so just type space:

#!/bin/bash
#Filname: CondConst.sh

if [ $UID -ne 0 ]; then
        echo "Non root User."
else
        echo "Root User."
fi
| improve this answer | |
  • 1
    Thank you also for having clarified to me why the System shows me [1000. – istackoverflow Dec 7 '16 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.