Check for root access in a bash script

Handy bit of code to add at the start of your bash script if you need to check for root access:

# Do we have root access?
if [[ $(/usr/bin/id -u) -ne 0 ]];
then echo "Sorry, this script needs to run with root or sudo access"
exit
fi

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

STAKING $ETM — EARN $ET

Populating Database With Faker Library

Three very hard ideas

Code Words Day 5, with Everest Pipkin

How to Upload File to Google Drive from Linux Command Line

Catch a spy to enumerate all Rational numbers

S3 Select: Why Would You Use It ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praj Basnet

Praj Basnet

More from Medium

Bash Cheastsheet

Linux Commands Part One: Navigation

Bash for Beginners Part 14: Deleting Files and Directories

Command Line Essentials