General Question

bomyne's avatar

How do I get my bash script to fail if it's being run as root?

Asked by bomyne (636points) May 23rd, 2010

I’m writing a bash script and I want to make sure it fails to run if the root user is the one running it. How do I do this?

Observing members: 0 Composing members: 0

2 Answers

davidgro's avatar

if [ "$(id -u)" == "0" ]; then
echo "This script must not be run as root" 1>&2
exit 1

bomyne's avatar

Works perfectly, thank you.

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther