I'm writing a bash script where I want to exit if the user is not root. The conditional works fine, but the script does not exit.
[[ `id -u` == 0 ]] || (echo "Must be root to run script"; exit)
I've tried using &&
instead of ;
but neither work.
Best Answer
You could do that this way:
("ordinary" conditional expression with an arithmetic binary operator in the first statement), or:
(arithmetic evaluation for the first test).
Notice the change
()
->{}
- the curly brackets do not spawn a subshell. (Searchman bash
for "subshell".)