Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why does the following script display an "unexpected operator" message and fail when run with SH, but not with BASH.

if [ $UID -ne 0 ]
        echo "You must be root."
        exit 1
        echo "Open sesame."
        exit 0
share|improve this question
up vote 6 down vote accepted

Not all shells define a variable UID. This is a bash and zsh feature only. In other shells, the UID variable is not defined, so your test command expands to [ -ne 0 ] which is a syntax error.

A portable way to obtain the user ID is with the id utility.

if [ "$(id -u)" -ne 0 ]; then …
share|improve this answer
Thanks for the clarification! – filmoreha Feb 5 '13 at 0:50
Another note is that quoting "$UID" would prevent the expansion from causing a syntax error. – jordanm Feb 5 '13 at 3:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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