Artificial intelligent assistant

Why does sh display an error when bash does not? Why does the following script display an "unexpected operator" message and fail when run with `sh`, but not with `bash`. #!/bin/sh if [ $UID -ne 0 ] then echo "You must be root." exit 1 else echo "Open sesame." exit 0 fi

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 …

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 0ce518864fe3f83fae383ab1704ffcb3